addrconf.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740
  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. "Disabled Privacy Extensions on device %p(%s)\n",
  329. dev, 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. /* Rule 5: Prefer outgoing interface */
  859. if (hiscore.rule < 5) {
  860. if (daddr_dev == NULL ||
  861. daddr_dev == ifa_result->idev->dev)
  862. hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
  863. hiscore.rule++;
  864. }
  865. if (daddr_dev == NULL ||
  866. daddr_dev == ifa->idev->dev) {
  867. score.attrs |= IPV6_SADDR_SCORE_OIF;
  868. if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
  869. score.rule = 5;
  870. goto record_it;
  871. }
  872. } else {
  873. if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
  874. continue;
  875. }
  876. /* Rule 6: Prefer matching label */
  877. if (hiscore.rule < 6) {
  878. if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
  879. hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
  880. hiscore.rule++;
  881. }
  882. if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
  883. score.attrs |= IPV6_SADDR_SCORE_LABEL;
  884. if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
  885. score.rule = 6;
  886. goto record_it;
  887. }
  888. } else {
  889. if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
  890. continue;
  891. }
  892. #ifdef CONFIG_IPV6_PRIVACY
  893. /* Rule 7: Prefer public address
  894. * Note: prefer temprary address if use_tempaddr >= 2
  895. */
  896. if (hiscore.rule < 7) {
  897. if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
  898. (ifa_result->idev->cnf.use_tempaddr >= 2))
  899. hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
  900. hiscore.rule++;
  901. }
  902. if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
  903. (ifa->idev->cnf.use_tempaddr >= 2)) {
  904. score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
  905. if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
  906. score.rule = 7;
  907. goto record_it;
  908. }
  909. } else {
  910. if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
  911. continue;
  912. }
  913. #endif
  914. /* Rule 8: Use longest matching prefix */
  915. if (hiscore.rule < 8)
  916. hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
  917. score.rule++;
  918. score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
  919. if (score.matchlen > hiscore.matchlen) {
  920. score.rule = 8;
  921. goto record_it;
  922. }
  923. #if 0
  924. else if (score.matchlen < hiscore.matchlen)
  925. continue;
  926. #endif
  927. /* Final Rule: choose first available one */
  928. continue;
  929. record_it:
  930. if (ifa_result)
  931. in6_ifa_put(ifa_result);
  932. in6_ifa_hold(ifa);
  933. ifa_result = ifa;
  934. hiscore = score;
  935. }
  936. read_unlock_bh(&idev->lock);
  937. }
  938. read_unlock(&addrconf_lock);
  939. read_unlock(&dev_base_lock);
  940. if (!ifa_result)
  941. return -EADDRNOTAVAIL;
  942. ipv6_addr_copy(saddr, &ifa_result->addr);
  943. in6_ifa_put(ifa_result);
  944. return 0;
  945. }
  946. int ipv6_get_saddr(struct dst_entry *dst,
  947. struct in6_addr *daddr, struct in6_addr *saddr)
  948. {
  949. return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
  950. }
  951. int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
  952. {
  953. struct inet6_dev *idev;
  954. int err = -EADDRNOTAVAIL;
  955. read_lock(&addrconf_lock);
  956. if ((idev = __in6_dev_get(dev)) != NULL) {
  957. struct inet6_ifaddr *ifp;
  958. read_lock_bh(&idev->lock);
  959. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  960. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  961. ipv6_addr_copy(addr, &ifp->addr);
  962. err = 0;
  963. break;
  964. }
  965. }
  966. read_unlock_bh(&idev->lock);
  967. }
  968. read_unlock(&addrconf_lock);
  969. return err;
  970. }
  971. static int ipv6_count_addresses(struct inet6_dev *idev)
  972. {
  973. int cnt = 0;
  974. struct inet6_ifaddr *ifp;
  975. read_lock_bh(&idev->lock);
  976. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
  977. cnt++;
  978. read_unlock_bh(&idev->lock);
  979. return cnt;
  980. }
  981. int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
  982. {
  983. struct inet6_ifaddr * ifp;
  984. u8 hash = ipv6_addr_hash(addr);
  985. read_lock_bh(&addrconf_hash_lock);
  986. for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
  987. if (ipv6_addr_equal(&ifp->addr, addr) &&
  988. !(ifp->flags&IFA_F_TENTATIVE)) {
  989. if (dev == NULL || ifp->idev->dev == dev ||
  990. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
  991. break;
  992. }
  993. }
  994. read_unlock_bh(&addrconf_hash_lock);
  995. return ifp != NULL;
  996. }
  997. static
  998. int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
  999. {
  1000. struct inet6_ifaddr * ifp;
  1001. u8 hash = ipv6_addr_hash(addr);
  1002. for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
  1003. if (ipv6_addr_equal(&ifp->addr, addr)) {
  1004. if (dev == NULL || ifp->idev->dev == dev)
  1005. break;
  1006. }
  1007. }
  1008. return ifp != NULL;
  1009. }
  1010. struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
  1011. {
  1012. struct inet6_ifaddr * ifp;
  1013. u8 hash = ipv6_addr_hash(addr);
  1014. read_lock_bh(&addrconf_hash_lock);
  1015. for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
  1016. if (ipv6_addr_equal(&ifp->addr, addr)) {
  1017. if (dev == NULL || ifp->idev->dev == dev ||
  1018. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
  1019. in6_ifa_hold(ifp);
  1020. break;
  1021. }
  1022. }
  1023. }
  1024. read_unlock_bh(&addrconf_hash_lock);
  1025. return ifp;
  1026. }
  1027. int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
  1028. {
  1029. const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
  1030. const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
  1031. u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
  1032. u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
  1033. int sk_ipv6only = ipv6_only_sock(sk);
  1034. int sk2_ipv6only = inet_v6_ipv6only(sk2);
  1035. int addr_type = ipv6_addr_type(sk_rcv_saddr6);
  1036. int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
  1037. if (!sk2_rcv_saddr && !sk_ipv6only)
  1038. return 1;
  1039. if (addr_type2 == IPV6_ADDR_ANY &&
  1040. !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
  1041. return 1;
  1042. if (addr_type == IPV6_ADDR_ANY &&
  1043. !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
  1044. return 1;
  1045. if (sk2_rcv_saddr6 &&
  1046. ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
  1047. return 1;
  1048. if (addr_type == IPV6_ADDR_MAPPED &&
  1049. !sk2_ipv6only &&
  1050. (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
  1051. return 1;
  1052. return 0;
  1053. }
  1054. /* Gets referenced address, destroys ifaddr */
  1055. void addrconf_dad_failure(struct inet6_ifaddr *ifp)
  1056. {
  1057. if (net_ratelimit())
  1058. printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
  1059. if (ifp->flags&IFA_F_PERMANENT) {
  1060. spin_lock_bh(&ifp->lock);
  1061. addrconf_del_timer(ifp);
  1062. ifp->flags |= IFA_F_TENTATIVE;
  1063. spin_unlock_bh(&ifp->lock);
  1064. in6_ifa_put(ifp);
  1065. #ifdef CONFIG_IPV6_PRIVACY
  1066. } else if (ifp->flags&IFA_F_TEMPORARY) {
  1067. struct inet6_ifaddr *ifpub;
  1068. spin_lock_bh(&ifp->lock);
  1069. ifpub = ifp->ifpub;
  1070. if (ifpub) {
  1071. in6_ifa_hold(ifpub);
  1072. spin_unlock_bh(&ifp->lock);
  1073. ipv6_create_tempaddr(ifpub, ifp);
  1074. in6_ifa_put(ifpub);
  1075. } else {
  1076. spin_unlock_bh(&ifp->lock);
  1077. }
  1078. ipv6_del_addr(ifp);
  1079. #endif
  1080. } else
  1081. ipv6_del_addr(ifp);
  1082. }
  1083. /* Join to solicited addr multicast group. */
  1084. void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
  1085. {
  1086. struct in6_addr maddr;
  1087. if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1088. return;
  1089. addrconf_addr_solict_mult(addr, &maddr);
  1090. ipv6_dev_mc_inc(dev, &maddr);
  1091. }
  1092. void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
  1093. {
  1094. struct in6_addr maddr;
  1095. if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1096. return;
  1097. addrconf_addr_solict_mult(addr, &maddr);
  1098. __ipv6_dev_mc_dec(idev, &maddr);
  1099. }
  1100. static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
  1101. {
  1102. struct in6_addr addr;
  1103. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1104. if (ipv6_addr_any(&addr))
  1105. return;
  1106. ipv6_dev_ac_inc(ifp->idev->dev, &addr);
  1107. }
  1108. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
  1109. {
  1110. struct in6_addr addr;
  1111. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1112. if (ipv6_addr_any(&addr))
  1113. return;
  1114. __ipv6_dev_ac_dec(ifp->idev, &addr);
  1115. }
  1116. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
  1117. {
  1118. switch (dev->type) {
  1119. case ARPHRD_ETHER:
  1120. case ARPHRD_FDDI:
  1121. case ARPHRD_IEEE802_TR:
  1122. if (dev->addr_len != ETH_ALEN)
  1123. return -1;
  1124. memcpy(eui, dev->dev_addr, 3);
  1125. memcpy(eui + 5, dev->dev_addr + 3, 3);
  1126. /*
  1127. * The zSeries OSA network cards can be shared among various
  1128. * OS instances, but the OSA cards have only one MAC address.
  1129. * This leads to duplicate address conflicts in conjunction
  1130. * with IPv6 if more than one instance uses the same card.
  1131. *
  1132. * The driver for these cards can deliver a unique 16-bit
  1133. * identifier for each instance sharing the same card. It is
  1134. * placed instead of 0xFFFE in the interface identifier. The
  1135. * "u" bit of the interface identifier is not inverted in this
  1136. * case. Hence the resulting interface identifier has local
  1137. * scope according to RFC2373.
  1138. */
  1139. if (dev->dev_id) {
  1140. eui[3] = (dev->dev_id >> 8) & 0xFF;
  1141. eui[4] = dev->dev_id & 0xFF;
  1142. } else {
  1143. eui[3] = 0xFF;
  1144. eui[4] = 0xFE;
  1145. eui[0] ^= 2;
  1146. }
  1147. return 0;
  1148. case ARPHRD_ARCNET:
  1149. /* XXX: inherit EUI-64 from other interface -- yoshfuji */
  1150. if (dev->addr_len != ARCNET_ALEN)
  1151. return -1;
  1152. memset(eui, 0, 7);
  1153. eui[7] = *(u8*)dev->dev_addr;
  1154. return 0;
  1155. case ARPHRD_INFINIBAND:
  1156. if (dev->addr_len != INFINIBAND_ALEN)
  1157. return -1;
  1158. memcpy(eui, dev->dev_addr + 12, 8);
  1159. eui[0] |= 2;
  1160. return 0;
  1161. }
  1162. return -1;
  1163. }
  1164. static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
  1165. {
  1166. int err = -1;
  1167. struct inet6_ifaddr *ifp;
  1168. read_lock_bh(&idev->lock);
  1169. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  1170. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  1171. memcpy(eui, ifp->addr.s6_addr+8, 8);
  1172. err = 0;
  1173. break;
  1174. }
  1175. }
  1176. read_unlock_bh(&idev->lock);
  1177. return err;
  1178. }
  1179. #ifdef CONFIG_IPV6_PRIVACY
  1180. /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
  1181. static int __ipv6_regen_rndid(struct inet6_dev *idev)
  1182. {
  1183. struct net_device *dev;
  1184. struct scatterlist sg[2];
  1185. sg_set_buf(&sg[0], idev->entropy, 8);
  1186. sg_set_buf(&sg[1], idev->work_eui64, 8);
  1187. dev = idev->dev;
  1188. if (ipv6_generate_eui64(idev->work_eui64, dev)) {
  1189. printk(KERN_INFO
  1190. "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
  1191. idev);
  1192. get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
  1193. }
  1194. regen:
  1195. spin_lock(&md5_tfm_lock);
  1196. if (unlikely(md5_tfm == NULL)) {
  1197. spin_unlock(&md5_tfm_lock);
  1198. return -1;
  1199. }
  1200. crypto_digest_init(md5_tfm);
  1201. crypto_digest_update(md5_tfm, sg, 2);
  1202. crypto_digest_final(md5_tfm, idev->work_digest);
  1203. spin_unlock(&md5_tfm_lock);
  1204. memcpy(idev->rndid, &idev->work_digest[0], 8);
  1205. idev->rndid[0] &= ~0x02;
  1206. memcpy(idev->entropy, &idev->work_digest[8], 8);
  1207. /*
  1208. * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
  1209. * check if generated address is not inappropriate
  1210. *
  1211. * - Reserved subnet anycast (RFC 2526)
  1212. * 11111101 11....11 1xxxxxxx
  1213. * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
  1214. * 00-00-5E-FE-xx-xx-xx-xx
  1215. * - value 0
  1216. * - XXX: already assigned to an address on the device
  1217. */
  1218. if (idev->rndid[0] == 0xfd &&
  1219. (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
  1220. (idev->rndid[7]&0x80))
  1221. goto regen;
  1222. if ((idev->rndid[0]|idev->rndid[1]) == 0) {
  1223. if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
  1224. goto regen;
  1225. if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
  1226. goto regen;
  1227. }
  1228. return 0;
  1229. }
  1230. static void ipv6_regen_rndid(unsigned long data)
  1231. {
  1232. struct inet6_dev *idev = (struct inet6_dev *) data;
  1233. unsigned long expires;
  1234. read_lock_bh(&addrconf_lock);
  1235. write_lock_bh(&idev->lock);
  1236. if (idev->dead)
  1237. goto out;
  1238. if (__ipv6_regen_rndid(idev) < 0)
  1239. goto out;
  1240. expires = jiffies +
  1241. idev->cnf.temp_prefered_lft * HZ -
  1242. idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
  1243. if (time_before(expires, jiffies)) {
  1244. printk(KERN_WARNING
  1245. "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
  1246. idev->dev->name);
  1247. goto out;
  1248. }
  1249. if (!mod_timer(&idev->regen_timer, expires))
  1250. in6_dev_hold(idev);
  1251. out:
  1252. write_unlock_bh(&idev->lock);
  1253. read_unlock_bh(&addrconf_lock);
  1254. in6_dev_put(idev);
  1255. }
  1256. static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
  1257. int ret = 0;
  1258. if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
  1259. ret = __ipv6_regen_rndid(idev);
  1260. return ret;
  1261. }
  1262. #endif
  1263. /*
  1264. * Add prefix route.
  1265. */
  1266. static void
  1267. addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
  1268. unsigned long expires, u32 flags)
  1269. {
  1270. struct in6_rtmsg rtmsg;
  1271. memset(&rtmsg, 0, sizeof(rtmsg));
  1272. ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
  1273. rtmsg.rtmsg_dst_len = plen;
  1274. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  1275. rtmsg.rtmsg_ifindex = dev->ifindex;
  1276. rtmsg.rtmsg_info = expires;
  1277. rtmsg.rtmsg_flags = RTF_UP|flags;
  1278. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  1279. /* Prevent useless cloning on PtP SIT.
  1280. This thing is done here expecting that the whole
  1281. class of non-broadcast devices need not cloning.
  1282. */
  1283. if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
  1284. rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
  1285. ip6_route_add(&rtmsg, NULL, NULL, NULL);
  1286. }
  1287. /* Create "default" multicast route to the interface */
  1288. static void addrconf_add_mroute(struct net_device *dev)
  1289. {
  1290. struct in6_rtmsg rtmsg;
  1291. memset(&rtmsg, 0, sizeof(rtmsg));
  1292. ipv6_addr_set(&rtmsg.rtmsg_dst,
  1293. htonl(0xFF000000), 0, 0, 0);
  1294. rtmsg.rtmsg_dst_len = 8;
  1295. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  1296. rtmsg.rtmsg_ifindex = dev->ifindex;
  1297. rtmsg.rtmsg_flags = RTF_UP;
  1298. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  1299. ip6_route_add(&rtmsg, NULL, NULL, NULL);
  1300. }
  1301. static void sit_route_add(struct net_device *dev)
  1302. {
  1303. struct in6_rtmsg rtmsg;
  1304. memset(&rtmsg, 0, sizeof(rtmsg));
  1305. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  1306. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  1307. /* prefix length - 96 bits "::d.d.d.d" */
  1308. rtmsg.rtmsg_dst_len = 96;
  1309. rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
  1310. rtmsg.rtmsg_ifindex = dev->ifindex;
  1311. ip6_route_add(&rtmsg, NULL, NULL, NULL);
  1312. }
  1313. static void addrconf_add_lroute(struct net_device *dev)
  1314. {
  1315. struct in6_addr addr;
  1316. ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
  1317. addrconf_prefix_route(&addr, 64, dev, 0, 0);
  1318. }
  1319. static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
  1320. {
  1321. struct inet6_dev *idev;
  1322. ASSERT_RTNL();
  1323. if ((idev = ipv6_find_idev(dev)) == NULL)
  1324. return NULL;
  1325. /* Add default multicast route */
  1326. addrconf_add_mroute(dev);
  1327. /* Add link local route */
  1328. addrconf_add_lroute(dev);
  1329. return idev;
  1330. }
  1331. void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
  1332. {
  1333. struct prefix_info *pinfo;
  1334. __u32 valid_lft;
  1335. __u32 prefered_lft;
  1336. int addr_type;
  1337. unsigned long rt_expires;
  1338. struct inet6_dev *in6_dev;
  1339. pinfo = (struct prefix_info *) opt;
  1340. if (len < sizeof(struct prefix_info)) {
  1341. ADBG(("addrconf: prefix option too short\n"));
  1342. return;
  1343. }
  1344. /*
  1345. * Validation checks ([ADDRCONF], page 19)
  1346. */
  1347. addr_type = ipv6_addr_type(&pinfo->prefix);
  1348. if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
  1349. return;
  1350. valid_lft = ntohl(pinfo->valid);
  1351. prefered_lft = ntohl(pinfo->prefered);
  1352. if (prefered_lft > valid_lft) {
  1353. if (net_ratelimit())
  1354. printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
  1355. return;
  1356. }
  1357. in6_dev = in6_dev_get(dev);
  1358. if (in6_dev == NULL) {
  1359. if (net_ratelimit())
  1360. printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
  1361. return;
  1362. }
  1363. /*
  1364. * Two things going on here:
  1365. * 1) Add routes for on-link prefixes
  1366. * 2) Configure prefixes with the auto flag set
  1367. */
  1368. /* Avoid arithmetic overflow. Really, we could
  1369. save rt_expires in seconds, likely valid_lft,
  1370. but it would require division in fib gc, that it
  1371. not good.
  1372. */
  1373. if (valid_lft >= 0x7FFFFFFF/HZ)
  1374. rt_expires = 0;
  1375. else
  1376. rt_expires = jiffies + valid_lft * HZ;
  1377. if (pinfo->onlink) {
  1378. struct rt6_info *rt;
  1379. rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
  1380. if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
  1381. if (rt->rt6i_flags&RTF_EXPIRES) {
  1382. if (valid_lft == 0) {
  1383. ip6_del_rt(rt, NULL, NULL, NULL);
  1384. rt = NULL;
  1385. } else {
  1386. rt->rt6i_expires = rt_expires;
  1387. }
  1388. }
  1389. } else if (valid_lft) {
  1390. addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
  1391. dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
  1392. }
  1393. if (rt)
  1394. dst_release(&rt->u.dst);
  1395. }
  1396. /* Try to figure out our local address for this prefix */
  1397. if (pinfo->autoconf && in6_dev->cnf.autoconf) {
  1398. struct inet6_ifaddr * ifp;
  1399. struct in6_addr addr;
  1400. int create = 0, update_lft = 0;
  1401. if (pinfo->prefix_len == 64) {
  1402. memcpy(&addr, &pinfo->prefix, 8);
  1403. if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
  1404. ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
  1405. in6_dev_put(in6_dev);
  1406. return;
  1407. }
  1408. goto ok;
  1409. }
  1410. if (net_ratelimit())
  1411. printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
  1412. pinfo->prefix_len);
  1413. in6_dev_put(in6_dev);
  1414. return;
  1415. ok:
  1416. ifp = ipv6_get_ifaddr(&addr, dev, 1);
  1417. if (ifp == NULL && valid_lft) {
  1418. int max_addresses = in6_dev->cnf.max_addresses;
  1419. /* Do not allow to create too much of autoconfigured
  1420. * addresses; this would be too easy way to crash kernel.
  1421. */
  1422. if (!max_addresses ||
  1423. ipv6_count_addresses(in6_dev) < max_addresses)
  1424. ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
  1425. addr_type&IPV6_ADDR_SCOPE_MASK, 0);
  1426. if (!ifp || IS_ERR(ifp)) {
  1427. in6_dev_put(in6_dev);
  1428. return;
  1429. }
  1430. update_lft = create = 1;
  1431. ifp->cstamp = jiffies;
  1432. addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
  1433. }
  1434. if (ifp) {
  1435. int flags;
  1436. unsigned long now;
  1437. #ifdef CONFIG_IPV6_PRIVACY
  1438. struct inet6_ifaddr *ift;
  1439. #endif
  1440. u32 stored_lft;
  1441. /* update lifetime (RFC2462 5.5.3 e) */
  1442. spin_lock(&ifp->lock);
  1443. now = jiffies;
  1444. if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
  1445. stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
  1446. else
  1447. stored_lft = 0;
  1448. if (!update_lft && stored_lft) {
  1449. if (valid_lft > MIN_VALID_LIFETIME ||
  1450. valid_lft > stored_lft)
  1451. update_lft = 1;
  1452. else if (stored_lft <= MIN_VALID_LIFETIME) {
  1453. /* valid_lft <= stored_lft is always true */
  1454. /* XXX: IPsec */
  1455. update_lft = 0;
  1456. } else {
  1457. valid_lft = MIN_VALID_LIFETIME;
  1458. if (valid_lft < prefered_lft)
  1459. prefered_lft = valid_lft;
  1460. update_lft = 1;
  1461. }
  1462. }
  1463. if (update_lft) {
  1464. ifp->valid_lft = valid_lft;
  1465. ifp->prefered_lft = prefered_lft;
  1466. ifp->tstamp = now;
  1467. flags = ifp->flags;
  1468. ifp->flags &= ~IFA_F_DEPRECATED;
  1469. spin_unlock(&ifp->lock);
  1470. if (!(flags&IFA_F_TENTATIVE))
  1471. ipv6_ifa_notify(0, ifp);
  1472. } else
  1473. spin_unlock(&ifp->lock);
  1474. #ifdef CONFIG_IPV6_PRIVACY
  1475. read_lock_bh(&in6_dev->lock);
  1476. /* update all temporary addresses in the list */
  1477. for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
  1478. /*
  1479. * When adjusting the lifetimes of an existing
  1480. * temporary address, only lower the lifetimes.
  1481. * Implementations must not increase the
  1482. * lifetimes of an existing temporary address
  1483. * when processing a Prefix Information Option.
  1484. */
  1485. spin_lock(&ift->lock);
  1486. flags = ift->flags;
  1487. if (ift->valid_lft > valid_lft &&
  1488. ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
  1489. ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
  1490. if (ift->prefered_lft > prefered_lft &&
  1491. ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
  1492. ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
  1493. spin_unlock(&ift->lock);
  1494. if (!(flags&IFA_F_TENTATIVE))
  1495. ipv6_ifa_notify(0, ift);
  1496. }
  1497. if (create && in6_dev->cnf.use_tempaddr > 0) {
  1498. /*
  1499. * When a new public address is created as described in [ADDRCONF],
  1500. * also create a new temporary address.
  1501. */
  1502. read_unlock_bh(&in6_dev->lock);
  1503. ipv6_create_tempaddr(ifp, NULL);
  1504. } else {
  1505. read_unlock_bh(&in6_dev->lock);
  1506. }
  1507. #endif
  1508. in6_ifa_put(ifp);
  1509. addrconf_verify(0);
  1510. }
  1511. }
  1512. inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
  1513. in6_dev_put(in6_dev);
  1514. }
  1515. /*
  1516. * Set destination address.
  1517. * Special case for SIT interfaces where we create a new "virtual"
  1518. * device.
  1519. */
  1520. int addrconf_set_dstaddr(void __user *arg)
  1521. {
  1522. struct in6_ifreq ireq;
  1523. struct net_device *dev;
  1524. int err = -EINVAL;
  1525. rtnl_lock();
  1526. err = -EFAULT;
  1527. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  1528. goto err_exit;
  1529. dev = __dev_get_by_index(ireq.ifr6_ifindex);
  1530. err = -ENODEV;
  1531. if (dev == NULL)
  1532. goto err_exit;
  1533. if (dev->type == ARPHRD_SIT) {
  1534. struct ifreq ifr;
  1535. mm_segment_t oldfs;
  1536. struct ip_tunnel_parm p;
  1537. err = -EADDRNOTAVAIL;
  1538. if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
  1539. goto err_exit;
  1540. memset(&p, 0, sizeof(p));
  1541. p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
  1542. p.iph.saddr = 0;
  1543. p.iph.version = 4;
  1544. p.iph.ihl = 5;
  1545. p.iph.protocol = IPPROTO_IPV6;
  1546. p.iph.ttl = 64;
  1547. ifr.ifr_ifru.ifru_data = (void __user *)&p;
  1548. oldfs = get_fs(); set_fs(KERNEL_DS);
  1549. err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  1550. set_fs(oldfs);
  1551. if (err == 0) {
  1552. err = -ENOBUFS;
  1553. if ((dev = __dev_get_by_name(p.name)) == NULL)
  1554. goto err_exit;
  1555. err = dev_open(dev);
  1556. }
  1557. }
  1558. err_exit:
  1559. rtnl_unlock();
  1560. return err;
  1561. }
  1562. /*
  1563. * Manual configuration of address on an interface
  1564. */
  1565. static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
  1566. {
  1567. struct inet6_ifaddr *ifp;
  1568. struct inet6_dev *idev;
  1569. struct net_device *dev;
  1570. int scope;
  1571. ASSERT_RTNL();
  1572. if ((dev = __dev_get_by_index(ifindex)) == NULL)
  1573. return -ENODEV;
  1574. if (!(dev->flags&IFF_UP))
  1575. return -ENETDOWN;
  1576. if ((idev = addrconf_add_dev(dev)) == NULL)
  1577. return -ENOBUFS;
  1578. scope = ipv6_addr_scope(pfx);
  1579. ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
  1580. if (!IS_ERR(ifp)) {
  1581. addrconf_dad_start(ifp, 0);
  1582. in6_ifa_put(ifp);
  1583. return 0;
  1584. }
  1585. return PTR_ERR(ifp);
  1586. }
  1587. static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
  1588. {
  1589. struct inet6_ifaddr *ifp;
  1590. struct inet6_dev *idev;
  1591. struct net_device *dev;
  1592. if ((dev = __dev_get_by_index(ifindex)) == NULL)
  1593. return -ENODEV;
  1594. if ((idev = __in6_dev_get(dev)) == NULL)
  1595. return -ENXIO;
  1596. read_lock_bh(&idev->lock);
  1597. for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
  1598. if (ifp->prefix_len == plen &&
  1599. ipv6_addr_equal(pfx, &ifp->addr)) {
  1600. in6_ifa_hold(ifp);
  1601. read_unlock_bh(&idev->lock);
  1602. ipv6_del_addr(ifp);
  1603. /* If the last address is deleted administratively,
  1604. disable IPv6 on this interface.
  1605. */
  1606. if (idev->addr_list == NULL)
  1607. addrconf_ifdown(idev->dev, 1);
  1608. return 0;
  1609. }
  1610. }
  1611. read_unlock_bh(&idev->lock);
  1612. return -EADDRNOTAVAIL;
  1613. }
  1614. int addrconf_add_ifaddr(void __user *arg)
  1615. {
  1616. struct in6_ifreq ireq;
  1617. int err;
  1618. if (!capable(CAP_NET_ADMIN))
  1619. return -EPERM;
  1620. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  1621. return -EFAULT;
  1622. rtnl_lock();
  1623. err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
  1624. rtnl_unlock();
  1625. return err;
  1626. }
  1627. int addrconf_del_ifaddr(void __user *arg)
  1628. {
  1629. struct in6_ifreq ireq;
  1630. int err;
  1631. if (!capable(CAP_NET_ADMIN))
  1632. return -EPERM;
  1633. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  1634. return -EFAULT;
  1635. rtnl_lock();
  1636. err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
  1637. rtnl_unlock();
  1638. return err;
  1639. }
  1640. static void sit_add_v4_addrs(struct inet6_dev *idev)
  1641. {
  1642. struct inet6_ifaddr * ifp;
  1643. struct in6_addr addr;
  1644. struct net_device *dev;
  1645. int scope;
  1646. ASSERT_RTNL();
  1647. memset(&addr, 0, sizeof(struct in6_addr));
  1648. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  1649. if (idev->dev->flags&IFF_POINTOPOINT) {
  1650. addr.s6_addr32[0] = htonl(0xfe800000);
  1651. scope = IFA_LINK;
  1652. } else {
  1653. scope = IPV6_ADDR_COMPATv4;
  1654. }
  1655. if (addr.s6_addr32[3]) {
  1656. ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
  1657. if (!IS_ERR(ifp)) {
  1658. spin_lock_bh(&ifp->lock);
  1659. ifp->flags &= ~IFA_F_TENTATIVE;
  1660. spin_unlock_bh(&ifp->lock);
  1661. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1662. in6_ifa_put(ifp);
  1663. }
  1664. return;
  1665. }
  1666. for (dev = dev_base; dev != NULL; dev = dev->next) {
  1667. struct in_device * in_dev = __in_dev_get_rtnl(dev);
  1668. if (in_dev && (dev->flags & IFF_UP)) {
  1669. struct in_ifaddr * ifa;
  1670. int flag = scope;
  1671. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  1672. int plen;
  1673. addr.s6_addr32[3] = ifa->ifa_local;
  1674. if (ifa->ifa_scope == RT_SCOPE_LINK)
  1675. continue;
  1676. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  1677. if (idev->dev->flags&IFF_POINTOPOINT)
  1678. continue;
  1679. flag |= IFA_HOST;
  1680. }
  1681. if (idev->dev->flags&IFF_POINTOPOINT)
  1682. plen = 64;
  1683. else
  1684. plen = 96;
  1685. ifp = ipv6_add_addr(idev, &addr, plen, flag,
  1686. IFA_F_PERMANENT);
  1687. if (!IS_ERR(ifp)) {
  1688. spin_lock_bh(&ifp->lock);
  1689. ifp->flags &= ~IFA_F_TENTATIVE;
  1690. spin_unlock_bh(&ifp->lock);
  1691. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1692. in6_ifa_put(ifp);
  1693. }
  1694. }
  1695. }
  1696. }
  1697. }
  1698. static void init_loopback(struct net_device *dev)
  1699. {
  1700. struct inet6_dev *idev;
  1701. struct inet6_ifaddr * ifp;
  1702. /* ::1 */
  1703. ASSERT_RTNL();
  1704. if ((idev = ipv6_find_idev(dev)) == NULL) {
  1705. printk(KERN_DEBUG "init loopback: add_dev failed\n");
  1706. return;
  1707. }
  1708. ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
  1709. if (!IS_ERR(ifp)) {
  1710. spin_lock_bh(&ifp->lock);
  1711. ifp->flags &= ~IFA_F_TENTATIVE;
  1712. spin_unlock_bh(&ifp->lock);
  1713. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1714. in6_ifa_put(ifp);
  1715. }
  1716. }
  1717. static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
  1718. {
  1719. struct inet6_ifaddr * ifp;
  1720. ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
  1721. if (!IS_ERR(ifp)) {
  1722. addrconf_dad_start(ifp, 0);
  1723. in6_ifa_put(ifp);
  1724. }
  1725. }
  1726. static void addrconf_dev_config(struct net_device *dev)
  1727. {
  1728. struct in6_addr addr;
  1729. struct inet6_dev * idev;
  1730. ASSERT_RTNL();
  1731. if ((dev->type != ARPHRD_ETHER) &&
  1732. (dev->type != ARPHRD_FDDI) &&
  1733. (dev->type != ARPHRD_IEEE802_TR) &&
  1734. (dev->type != ARPHRD_ARCNET) &&
  1735. (dev->type != ARPHRD_INFINIBAND)) {
  1736. /* Alas, we support only Ethernet autoconfiguration. */
  1737. return;
  1738. }
  1739. idev = addrconf_add_dev(dev);
  1740. if (idev == NULL)
  1741. return;
  1742. memset(&addr, 0, sizeof(struct in6_addr));
  1743. addr.s6_addr32[0] = htonl(0xFE800000);
  1744. if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
  1745. addrconf_add_linklocal(idev, &addr);
  1746. }
  1747. static void addrconf_sit_config(struct net_device *dev)
  1748. {
  1749. struct inet6_dev *idev;
  1750. ASSERT_RTNL();
  1751. /*
  1752. * Configure the tunnel with one of our IPv4
  1753. * addresses... we should configure all of
  1754. * our v4 addrs in the tunnel
  1755. */
  1756. if ((idev = ipv6_find_idev(dev)) == NULL) {
  1757. printk(KERN_DEBUG "init sit: add_dev failed\n");
  1758. return;
  1759. }
  1760. sit_add_v4_addrs(idev);
  1761. if (dev->flags&IFF_POINTOPOINT) {
  1762. addrconf_add_mroute(dev);
  1763. addrconf_add_lroute(dev);
  1764. } else
  1765. sit_route_add(dev);
  1766. }
  1767. static inline int
  1768. ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
  1769. {
  1770. struct in6_addr lladdr;
  1771. if (!ipv6_get_lladdr(link_dev, &lladdr)) {
  1772. addrconf_add_linklocal(idev, &lladdr);
  1773. return 0;
  1774. }
  1775. return -1;
  1776. }
  1777. static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
  1778. {
  1779. struct net_device *link_dev;
  1780. /* first try to inherit the link-local address from the link device */
  1781. if (idev->dev->iflink &&
  1782. (link_dev = __dev_get_by_index(idev->dev->iflink))) {
  1783. if (!ipv6_inherit_linklocal(idev, link_dev))
  1784. return;
  1785. }
  1786. /* then try to inherit it from any device */
  1787. for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
  1788. if (!ipv6_inherit_linklocal(idev, link_dev))
  1789. return;
  1790. }
  1791. printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
  1792. }
  1793. /*
  1794. * Autoconfigure tunnel with a link-local address so routing protocols,
  1795. * DHCPv6, MLD etc. can be run over the virtual link
  1796. */
  1797. static void addrconf_ip6_tnl_config(struct net_device *dev)
  1798. {
  1799. struct inet6_dev *idev;
  1800. ASSERT_RTNL();
  1801. if ((idev = addrconf_add_dev(dev)) == NULL) {
  1802. printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
  1803. return;
  1804. }
  1805. ip6_tnl_add_linklocal(idev);
  1806. addrconf_add_mroute(dev);
  1807. }
  1808. static int addrconf_notify(struct notifier_block *this, unsigned long event,
  1809. void * data)
  1810. {
  1811. struct net_device *dev = (struct net_device *) data;
  1812. struct inet6_dev *idev = __in6_dev_get(dev);
  1813. switch(event) {
  1814. case NETDEV_UP:
  1815. switch(dev->type) {
  1816. case ARPHRD_SIT:
  1817. addrconf_sit_config(dev);
  1818. break;
  1819. case ARPHRD_TUNNEL6:
  1820. addrconf_ip6_tnl_config(dev);
  1821. break;
  1822. case ARPHRD_LOOPBACK:
  1823. init_loopback(dev);
  1824. break;
  1825. default:
  1826. addrconf_dev_config(dev);
  1827. break;
  1828. };
  1829. if (idev) {
  1830. /* If the MTU changed during the interface down, when the
  1831. interface up, the changed MTU must be reflected in the
  1832. idev as well as routers.
  1833. */
  1834. if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
  1835. rt6_mtu_change(dev, dev->mtu);
  1836. idev->cnf.mtu6 = dev->mtu;
  1837. }
  1838. idev->tstamp = jiffies;
  1839. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1840. /* If the changed mtu during down is lower than IPV6_MIN_MTU
  1841. stop IPv6 on this interface.
  1842. */
  1843. if (dev->mtu < IPV6_MIN_MTU)
  1844. addrconf_ifdown(dev, event != NETDEV_DOWN);
  1845. }
  1846. break;
  1847. case NETDEV_CHANGEMTU:
  1848. if ( idev && dev->mtu >= IPV6_MIN_MTU) {
  1849. rt6_mtu_change(dev, dev->mtu);
  1850. idev->cnf.mtu6 = dev->mtu;
  1851. break;
  1852. }
  1853. /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
  1854. case NETDEV_DOWN:
  1855. case NETDEV_UNREGISTER:
  1856. /*
  1857. * Remove all addresses from this interface.
  1858. */
  1859. addrconf_ifdown(dev, event != NETDEV_DOWN);
  1860. break;
  1861. case NETDEV_CHANGE:
  1862. break;
  1863. case NETDEV_CHANGENAME:
  1864. #ifdef CONFIG_SYSCTL
  1865. if (idev) {
  1866. addrconf_sysctl_unregister(&idev->cnf);
  1867. neigh_sysctl_unregister(idev->nd_parms);
  1868. neigh_sysctl_register(dev, idev->nd_parms,
  1869. NET_IPV6, NET_IPV6_NEIGH, "ipv6",
  1870. &ndisc_ifinfo_sysctl_change,
  1871. NULL);
  1872. addrconf_sysctl_register(idev, &idev->cnf);
  1873. }
  1874. #endif
  1875. break;
  1876. };
  1877. return NOTIFY_OK;
  1878. }
  1879. /*
  1880. * addrconf module should be notified of a device going up
  1881. */
  1882. static struct notifier_block ipv6_dev_notf = {
  1883. .notifier_call = addrconf_notify,
  1884. .priority = 0
  1885. };
  1886. static int addrconf_ifdown(struct net_device *dev, int how)
  1887. {
  1888. struct inet6_dev *idev;
  1889. struct inet6_ifaddr *ifa, **bifa;
  1890. int i;
  1891. ASSERT_RTNL();
  1892. if (dev == &loopback_dev && how == 1)
  1893. how = 0;
  1894. rt6_ifdown(dev);
  1895. neigh_ifdown(&nd_tbl, dev);
  1896. idev = __in6_dev_get(dev);
  1897. if (idev == NULL)
  1898. return -ENODEV;
  1899. /* Step 1: remove reference to ipv6 device from parent device.
  1900. Do not dev_put!
  1901. */
  1902. if (how == 1) {
  1903. write_lock_bh(&addrconf_lock);
  1904. dev->ip6_ptr = NULL;
  1905. idev->dead = 1;
  1906. write_unlock_bh(&addrconf_lock);
  1907. /* Step 1.5: remove snmp6 entry */
  1908. snmp6_unregister_dev(idev);
  1909. }
  1910. /* Step 2: clear hash table */
  1911. for (i=0; i<IN6_ADDR_HSIZE; i++) {
  1912. bifa = &inet6_addr_lst[i];
  1913. write_lock_bh(&addrconf_hash_lock);
  1914. while ((ifa = *bifa) != NULL) {
  1915. if (ifa->idev == idev) {
  1916. *bifa = ifa->lst_next;
  1917. ifa->lst_next = NULL;
  1918. addrconf_del_timer(ifa);
  1919. in6_ifa_put(ifa);
  1920. continue;
  1921. }
  1922. bifa = &ifa->lst_next;
  1923. }
  1924. write_unlock_bh(&addrconf_hash_lock);
  1925. }
  1926. write_lock_bh(&idev->lock);
  1927. /* Step 3: clear flags for stateless addrconf */
  1928. if (how != 1)
  1929. idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
  1930. /* Step 4: clear address list */
  1931. #ifdef CONFIG_IPV6_PRIVACY
  1932. if (how == 1 && del_timer(&idev->regen_timer))
  1933. in6_dev_put(idev);
  1934. /* clear tempaddr list */
  1935. while ((ifa = idev->tempaddr_list) != NULL) {
  1936. idev->tempaddr_list = ifa->tmp_next;
  1937. ifa->tmp_next = NULL;
  1938. ifa->dead = 1;
  1939. write_unlock_bh(&idev->lock);
  1940. spin_lock_bh(&ifa->lock);
  1941. if (ifa->ifpub) {
  1942. in6_ifa_put(ifa->ifpub);
  1943. ifa->ifpub = NULL;
  1944. }
  1945. spin_unlock_bh(&ifa->lock);
  1946. in6_ifa_put(ifa);
  1947. write_lock_bh(&idev->lock);
  1948. }
  1949. #endif
  1950. while ((ifa = idev->addr_list) != NULL) {
  1951. idev->addr_list = ifa->if_next;
  1952. ifa->if_next = NULL;
  1953. ifa->dead = 1;
  1954. addrconf_del_timer(ifa);
  1955. write_unlock_bh(&idev->lock);
  1956. __ipv6_ifa_notify(RTM_DELADDR, ifa);
  1957. in6_ifa_put(ifa);
  1958. write_lock_bh(&idev->lock);
  1959. }
  1960. write_unlock_bh(&idev->lock);
  1961. /* Step 5: Discard multicast list */
  1962. if (how == 1)
  1963. ipv6_mc_destroy_dev(idev);
  1964. else
  1965. ipv6_mc_down(idev);
  1966. /* Step 5: netlink notification of this interface */
  1967. idev->tstamp = jiffies;
  1968. inet6_ifinfo_notify(RTM_DELLINK, idev);
  1969. /* Shot the device (if unregistered) */
  1970. if (how == 1) {
  1971. #ifdef CONFIG_SYSCTL
  1972. addrconf_sysctl_unregister(&idev->cnf);
  1973. neigh_sysctl_unregister(idev->nd_parms);
  1974. #endif
  1975. neigh_parms_release(&nd_tbl, idev->nd_parms);
  1976. neigh_ifdown(&nd_tbl, dev);
  1977. in6_dev_put(idev);
  1978. }
  1979. return 0;
  1980. }
  1981. static void addrconf_rs_timer(unsigned long data)
  1982. {
  1983. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  1984. if (ifp->idev->cnf.forwarding)
  1985. goto out;
  1986. if (ifp->idev->if_flags & IF_RA_RCVD) {
  1987. /*
  1988. * Announcement received after solicitation
  1989. * was sent
  1990. */
  1991. goto out;
  1992. }
  1993. spin_lock(&ifp->lock);
  1994. if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
  1995. struct in6_addr all_routers;
  1996. /* The wait after the last probe can be shorter */
  1997. addrconf_mod_timer(ifp, AC_RS,
  1998. (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
  1999. ifp->idev->cnf.rtr_solicit_delay :
  2000. ifp->idev->cnf.rtr_solicit_interval);
  2001. spin_unlock(&ifp->lock);
  2002. ipv6_addr_all_routers(&all_routers);
  2003. ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
  2004. } else {
  2005. spin_unlock(&ifp->lock);
  2006. /*
  2007. * Note: we do not support deprecated "all on-link"
  2008. * assumption any longer.
  2009. */
  2010. printk(KERN_DEBUG "%s: no IPv6 routers present\n",
  2011. ifp->idev->dev->name);
  2012. }
  2013. out:
  2014. in6_ifa_put(ifp);
  2015. }
  2016. /*
  2017. * Duplicate Address Detection
  2018. */
  2019. static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
  2020. {
  2021. struct inet6_dev *idev = ifp->idev;
  2022. struct net_device *dev = idev->dev;
  2023. unsigned long rand_num;
  2024. addrconf_join_solict(dev, &ifp->addr);
  2025. if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
  2026. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
  2027. flags);
  2028. net_srandom(ifp->addr.s6_addr32[3]);
  2029. rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
  2030. read_lock_bh(&idev->lock);
  2031. if (ifp->dead)
  2032. goto out;
  2033. spin_lock_bh(&ifp->lock);
  2034. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  2035. !(ifp->flags&IFA_F_TENTATIVE)) {
  2036. ifp->flags &= ~IFA_F_TENTATIVE;
  2037. spin_unlock_bh(&ifp->lock);
  2038. read_unlock_bh(&idev->lock);
  2039. addrconf_dad_completed(ifp);
  2040. return;
  2041. }
  2042. ifp->probes = idev->cnf.dad_transmits;
  2043. addrconf_mod_timer(ifp, AC_DAD, rand_num);
  2044. spin_unlock_bh(&ifp->lock);
  2045. out:
  2046. read_unlock_bh(&idev->lock);
  2047. }
  2048. static void addrconf_dad_timer(unsigned long data)
  2049. {
  2050. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  2051. struct inet6_dev *idev = ifp->idev;
  2052. struct in6_addr unspec;
  2053. struct in6_addr mcaddr;
  2054. read_lock_bh(&idev->lock);
  2055. if (idev->dead) {
  2056. read_unlock_bh(&idev->lock);
  2057. goto out;
  2058. }
  2059. spin_lock_bh(&ifp->lock);
  2060. if (ifp->probes == 0) {
  2061. /*
  2062. * DAD was successful
  2063. */
  2064. ifp->flags &= ~IFA_F_TENTATIVE;
  2065. spin_unlock_bh(&ifp->lock);
  2066. read_unlock_bh(&idev->lock);
  2067. addrconf_dad_completed(ifp);
  2068. goto out;
  2069. }
  2070. ifp->probes--;
  2071. addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
  2072. spin_unlock_bh(&ifp->lock);
  2073. read_unlock_bh(&idev->lock);
  2074. /* send a neighbour solicitation for our addr */
  2075. memset(&unspec, 0, sizeof(unspec));
  2076. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  2077. ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
  2078. out:
  2079. in6_ifa_put(ifp);
  2080. }
  2081. static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
  2082. {
  2083. struct net_device * dev = ifp->idev->dev;
  2084. /*
  2085. * Configure the address for reception. Now it is valid.
  2086. */
  2087. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2088. /* If added prefix is link local and forwarding is off,
  2089. start sending router solicitations.
  2090. */
  2091. if (ifp->idev->cnf.forwarding == 0 &&
  2092. ifp->idev->cnf.rtr_solicits > 0 &&
  2093. (dev->flags&IFF_LOOPBACK) == 0 &&
  2094. (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
  2095. struct in6_addr all_routers;
  2096. ipv6_addr_all_routers(&all_routers);
  2097. /*
  2098. * If a host as already performed a random delay
  2099. * [...] as part of DAD [...] there is no need
  2100. * to delay again before sending the first RS
  2101. */
  2102. ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
  2103. spin_lock_bh(&ifp->lock);
  2104. ifp->probes = 1;
  2105. ifp->idev->if_flags |= IF_RS_SENT;
  2106. addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
  2107. spin_unlock_bh(&ifp->lock);
  2108. }
  2109. }
  2110. #ifdef CONFIG_PROC_FS
  2111. struct if6_iter_state {
  2112. int bucket;
  2113. };
  2114. static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
  2115. {
  2116. struct inet6_ifaddr *ifa = NULL;
  2117. struct if6_iter_state *state = seq->private;
  2118. for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
  2119. ifa = inet6_addr_lst[state->bucket];
  2120. if (ifa)
  2121. break;
  2122. }
  2123. return ifa;
  2124. }
  2125. static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
  2126. {
  2127. struct if6_iter_state *state = seq->private;
  2128. ifa = ifa->lst_next;
  2129. try_again:
  2130. if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
  2131. ifa = inet6_addr_lst[state->bucket];
  2132. goto try_again;
  2133. }
  2134. return ifa;
  2135. }
  2136. static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
  2137. {
  2138. struct inet6_ifaddr *ifa = if6_get_first(seq);
  2139. if (ifa)
  2140. while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
  2141. --pos;
  2142. return pos ? NULL : ifa;
  2143. }
  2144. static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
  2145. {
  2146. read_lock_bh(&addrconf_hash_lock);
  2147. return if6_get_idx(seq, *pos);
  2148. }
  2149. static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2150. {
  2151. struct inet6_ifaddr *ifa;
  2152. ifa = if6_get_next(seq, v);
  2153. ++*pos;
  2154. return ifa;
  2155. }
  2156. static void if6_seq_stop(struct seq_file *seq, void *v)
  2157. {
  2158. read_unlock_bh(&addrconf_hash_lock);
  2159. }
  2160. static int if6_seq_show(struct seq_file *seq, void *v)
  2161. {
  2162. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
  2163. seq_printf(seq,
  2164. "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
  2165. NIP6(ifp->addr),
  2166. ifp->idev->dev->ifindex,
  2167. ifp->prefix_len,
  2168. ifp->scope,
  2169. ifp->flags,
  2170. ifp->idev->dev->name);
  2171. return 0;
  2172. }
  2173. static struct seq_operations if6_seq_ops = {
  2174. .start = if6_seq_start,
  2175. .next = if6_seq_next,
  2176. .show = if6_seq_show,
  2177. .stop = if6_seq_stop,
  2178. };
  2179. static int if6_seq_open(struct inode *inode, struct file *file)
  2180. {
  2181. struct seq_file *seq;
  2182. int rc = -ENOMEM;
  2183. struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2184. if (!s)
  2185. goto out;
  2186. memset(s, 0, sizeof(*s));
  2187. rc = seq_open(file, &if6_seq_ops);
  2188. if (rc)
  2189. goto out_kfree;
  2190. seq = file->private_data;
  2191. seq->private = s;
  2192. out:
  2193. return rc;
  2194. out_kfree:
  2195. kfree(s);
  2196. goto out;
  2197. }
  2198. static struct file_operations if6_fops = {
  2199. .owner = THIS_MODULE,
  2200. .open = if6_seq_open,
  2201. .read = seq_read,
  2202. .llseek = seq_lseek,
  2203. .release = seq_release_private,
  2204. };
  2205. int __init if6_proc_init(void)
  2206. {
  2207. if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
  2208. return -ENOMEM;
  2209. return 0;
  2210. }
  2211. void if6_proc_exit(void)
  2212. {
  2213. proc_net_remove("if_inet6");
  2214. }
  2215. #endif /* CONFIG_PROC_FS */
  2216. /*
  2217. * Periodic address status verification
  2218. */
  2219. static void addrconf_verify(unsigned long foo)
  2220. {
  2221. struct inet6_ifaddr *ifp;
  2222. unsigned long now, next;
  2223. int i;
  2224. spin_lock_bh(&addrconf_verify_lock);
  2225. now = jiffies;
  2226. next = now + ADDR_CHECK_FREQUENCY;
  2227. del_timer(&addr_chk_timer);
  2228. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  2229. restart:
  2230. write_lock(&addrconf_hash_lock);
  2231. for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
  2232. unsigned long age;
  2233. #ifdef CONFIG_IPV6_PRIVACY
  2234. unsigned long regen_advance;
  2235. #endif
  2236. if (ifp->flags & IFA_F_PERMANENT)
  2237. continue;
  2238. spin_lock(&ifp->lock);
  2239. age = (now - ifp->tstamp) / HZ;
  2240. #ifdef CONFIG_IPV6_PRIVACY
  2241. regen_advance = ifp->idev->cnf.regen_max_retry *
  2242. ifp->idev->cnf.dad_transmits *
  2243. ifp->idev->nd_parms->retrans_time / HZ;
  2244. #endif
  2245. if (age >= ifp->valid_lft) {
  2246. spin_unlock(&ifp->lock);
  2247. in6_ifa_hold(ifp);
  2248. write_unlock(&addrconf_hash_lock);
  2249. ipv6_del_addr(ifp);
  2250. goto restart;
  2251. } else if (age >= ifp->prefered_lft) {
  2252. /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
  2253. int deprecate = 0;
  2254. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  2255. deprecate = 1;
  2256. ifp->flags |= IFA_F_DEPRECATED;
  2257. }
  2258. if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
  2259. next = ifp->tstamp + ifp->valid_lft * HZ;
  2260. spin_unlock(&ifp->lock);
  2261. if (deprecate) {
  2262. in6_ifa_hold(ifp);
  2263. write_unlock(&addrconf_hash_lock);
  2264. ipv6_ifa_notify(0, ifp);
  2265. in6_ifa_put(ifp);
  2266. goto restart;
  2267. }
  2268. #ifdef CONFIG_IPV6_PRIVACY
  2269. } else if ((ifp->flags&IFA_F_TEMPORARY) &&
  2270. !(ifp->flags&IFA_F_TENTATIVE)) {
  2271. if (age >= ifp->prefered_lft - regen_advance) {
  2272. struct inet6_ifaddr *ifpub = ifp->ifpub;
  2273. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  2274. next = ifp->tstamp + ifp->prefered_lft * HZ;
  2275. if (!ifp->regen_count && ifpub) {
  2276. ifp->regen_count++;
  2277. in6_ifa_hold(ifp);
  2278. in6_ifa_hold(ifpub);
  2279. spin_unlock(&ifp->lock);
  2280. write_unlock(&addrconf_hash_lock);
  2281. ipv6_create_tempaddr(ifpub, ifp);
  2282. in6_ifa_put(ifpub);
  2283. in6_ifa_put(ifp);
  2284. goto restart;
  2285. }
  2286. } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
  2287. next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
  2288. spin_unlock(&ifp->lock);
  2289. #endif
  2290. } else {
  2291. /* ifp->prefered_lft <= ifp->valid_lft */
  2292. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  2293. next = ifp->tstamp + ifp->prefered_lft * HZ;
  2294. spin_unlock(&ifp->lock);
  2295. }
  2296. }
  2297. write_unlock(&addrconf_hash_lock);
  2298. }
  2299. addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
  2300. add_timer(&addr_chk_timer);
  2301. spin_unlock_bh(&addrconf_verify_lock);
  2302. }
  2303. static int
  2304. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  2305. {
  2306. struct rtattr **rta = arg;
  2307. struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
  2308. struct in6_addr *pfx;
  2309. pfx = NULL;
  2310. if (rta[IFA_ADDRESS-1]) {
  2311. if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
  2312. return -EINVAL;
  2313. pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
  2314. }
  2315. if (rta[IFA_LOCAL-1]) {
  2316. if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
  2317. return -EINVAL;
  2318. pfx = RTA_DATA(rta[IFA_LOCAL-1]);
  2319. }
  2320. if (pfx == NULL)
  2321. return -EINVAL;
  2322. return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
  2323. }
  2324. static int
  2325. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  2326. {
  2327. struct rtattr **rta = arg;
  2328. struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
  2329. struct in6_addr *pfx;
  2330. pfx = NULL;
  2331. if (rta[IFA_ADDRESS-1]) {
  2332. if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
  2333. return -EINVAL;
  2334. pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
  2335. }
  2336. if (rta[IFA_LOCAL-1]) {
  2337. if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
  2338. return -EINVAL;
  2339. pfx = RTA_DATA(rta[IFA_LOCAL-1]);
  2340. }
  2341. if (pfx == NULL)
  2342. return -EINVAL;
  2343. return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
  2344. }
  2345. static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
  2346. u32 pid, u32 seq, int event, unsigned int flags)
  2347. {
  2348. struct ifaddrmsg *ifm;
  2349. struct nlmsghdr *nlh;
  2350. struct ifa_cacheinfo ci;
  2351. unsigned char *b = skb->tail;
  2352. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
  2353. ifm = NLMSG_DATA(nlh);
  2354. ifm->ifa_family = AF_INET6;
  2355. ifm->ifa_prefixlen = ifa->prefix_len;
  2356. ifm->ifa_flags = ifa->flags;
  2357. ifm->ifa_scope = RT_SCOPE_UNIVERSE;
  2358. if (ifa->scope&IFA_HOST)
  2359. ifm->ifa_scope = RT_SCOPE_HOST;
  2360. else if (ifa->scope&IFA_LINK)
  2361. ifm->ifa_scope = RT_SCOPE_LINK;
  2362. else if (ifa->scope&IFA_SITE)
  2363. ifm->ifa_scope = RT_SCOPE_SITE;
  2364. ifm->ifa_index = ifa->idev->dev->ifindex;
  2365. RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
  2366. if (!(ifa->flags&IFA_F_PERMANENT)) {
  2367. ci.ifa_prefered = ifa->prefered_lft;
  2368. ci.ifa_valid = ifa->valid_lft;
  2369. if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
  2370. long tval = (jiffies - ifa->tstamp)/HZ;
  2371. ci.ifa_prefered -= tval;
  2372. if (ci.ifa_valid != INFINITY_LIFE_TIME)
  2373. ci.ifa_valid -= tval;
  2374. }
  2375. } else {
  2376. ci.ifa_prefered = INFINITY_LIFE_TIME;
  2377. ci.ifa_valid = INFINITY_LIFE_TIME;
  2378. }
  2379. ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
  2380. + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
  2381. ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
  2382. + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
  2383. RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  2384. nlh->nlmsg_len = skb->tail - b;
  2385. return skb->len;
  2386. nlmsg_failure:
  2387. rtattr_failure:
  2388. skb_trim(skb, b - skb->data);
  2389. return -1;
  2390. }
  2391. static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
  2392. u32 pid, u32 seq, int event, u16 flags)
  2393. {
  2394. struct ifaddrmsg *ifm;
  2395. struct nlmsghdr *nlh;
  2396. struct ifa_cacheinfo ci;
  2397. unsigned char *b = skb->tail;
  2398. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
  2399. ifm = NLMSG_DATA(nlh);
  2400. ifm->ifa_family = AF_INET6;
  2401. ifm->ifa_prefixlen = 128;
  2402. ifm->ifa_flags = IFA_F_PERMANENT;
  2403. ifm->ifa_scope = RT_SCOPE_UNIVERSE;
  2404. if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
  2405. ifm->ifa_scope = RT_SCOPE_SITE;
  2406. ifm->ifa_index = ifmca->idev->dev->ifindex;
  2407. RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
  2408. ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
  2409. * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
  2410. * 100 / HZ);
  2411. ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
  2412. * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
  2413. * 100 / HZ);
  2414. ci.ifa_prefered = INFINITY_LIFE_TIME;
  2415. ci.ifa_valid = INFINITY_LIFE_TIME;
  2416. RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  2417. nlh->nlmsg_len = skb->tail - b;
  2418. return skb->len;
  2419. nlmsg_failure:
  2420. rtattr_failure:
  2421. skb_trim(skb, b - skb->data);
  2422. return -1;
  2423. }
  2424. static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
  2425. u32 pid, u32 seq, int event, unsigned int flags)
  2426. {
  2427. struct ifaddrmsg *ifm;
  2428. struct nlmsghdr *nlh;
  2429. struct ifa_cacheinfo ci;
  2430. unsigned char *b = skb->tail;
  2431. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
  2432. ifm = NLMSG_DATA(nlh);
  2433. ifm->ifa_family = AF_INET6;
  2434. ifm->ifa_prefixlen = 128;
  2435. ifm->ifa_flags = IFA_F_PERMANENT;
  2436. ifm->ifa_scope = RT_SCOPE_UNIVERSE;
  2437. if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
  2438. ifm->ifa_scope = RT_SCOPE_SITE;
  2439. ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
  2440. RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
  2441. ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
  2442. * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
  2443. * 100 / HZ);
  2444. ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
  2445. * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
  2446. * 100 / HZ);
  2447. ci.ifa_prefered = INFINITY_LIFE_TIME;
  2448. ci.ifa_valid = INFINITY_LIFE_TIME;
  2449. RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  2450. nlh->nlmsg_len = skb->tail - b;
  2451. return skb->len;
  2452. nlmsg_failure:
  2453. rtattr_failure:
  2454. skb_trim(skb, b - skb->data);
  2455. return -1;
  2456. }
  2457. enum addr_type_t
  2458. {
  2459. UNICAST_ADDR,
  2460. MULTICAST_ADDR,
  2461. ANYCAST_ADDR,
  2462. };
  2463. static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
  2464. enum addr_type_t type)
  2465. {
  2466. int idx, ip_idx;
  2467. int s_idx, s_ip_idx;
  2468. int err = 1;
  2469. struct net_device *dev;
  2470. struct inet6_dev *idev = NULL;
  2471. struct inet6_ifaddr *ifa;
  2472. struct ifmcaddr6 *ifmca;
  2473. struct ifacaddr6 *ifaca;
  2474. s_idx = cb->args[0];
  2475. s_ip_idx = ip_idx = cb->args[1];
  2476. read_lock(&dev_base_lock);
  2477. for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
  2478. if (idx < s_idx)
  2479. continue;
  2480. if (idx > s_idx)
  2481. s_ip_idx = 0;
  2482. ip_idx = 0;
  2483. if ((idev = in6_dev_get(dev)) == NULL)
  2484. continue;
  2485. read_lock_bh(&idev->lock);
  2486. switch (type) {
  2487. case UNICAST_ADDR:
  2488. /* unicast address incl. temp addr */
  2489. for (ifa = idev->addr_list; ifa;
  2490. ifa = ifa->if_next, ip_idx++) {
  2491. if (ip_idx < s_ip_idx)
  2492. continue;
  2493. if ((err = inet6_fill_ifaddr(skb, ifa,
  2494. NETLINK_CB(cb->skb).pid,
  2495. cb->nlh->nlmsg_seq, RTM_NEWADDR,
  2496. NLM_F_MULTI)) <= 0)
  2497. goto done;
  2498. }
  2499. break;
  2500. case MULTICAST_ADDR:
  2501. /* multicast address */
  2502. for (ifmca = idev->mc_list; ifmca;
  2503. ifmca = ifmca->next, ip_idx++) {
  2504. if (ip_idx < s_ip_idx)
  2505. continue;
  2506. if ((err = inet6_fill_ifmcaddr(skb, ifmca,
  2507. NETLINK_CB(cb->skb).pid,
  2508. cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
  2509. NLM_F_MULTI)) <= 0)
  2510. goto done;
  2511. }
  2512. break;
  2513. case ANYCAST_ADDR:
  2514. /* anycast address */
  2515. for (ifaca = idev->ac_list; ifaca;
  2516. ifaca = ifaca->aca_next, ip_idx++) {
  2517. if (ip_idx < s_ip_idx)
  2518. continue;
  2519. if ((err = inet6_fill_ifacaddr(skb, ifaca,
  2520. NETLINK_CB(cb->skb).pid,
  2521. cb->nlh->nlmsg_seq, RTM_GETANYCAST,
  2522. NLM_F_MULTI)) <= 0)
  2523. goto done;
  2524. }
  2525. break;
  2526. default:
  2527. break;
  2528. }
  2529. read_unlock_bh(&idev->lock);
  2530. in6_dev_put(idev);
  2531. }
  2532. done:
  2533. if (err <= 0) {
  2534. read_unlock_bh(&idev->lock);
  2535. in6_dev_put(idev);
  2536. }
  2537. read_unlock(&dev_base_lock);
  2538. cb->args[0] = idx;
  2539. cb->args[1] = ip_idx;
  2540. return skb->len;
  2541. }
  2542. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  2543. {
  2544. enum addr_type_t type = UNICAST_ADDR;
  2545. return inet6_dump_addr(skb, cb, type);
  2546. }
  2547. static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
  2548. {
  2549. enum addr_type_t type = MULTICAST_ADDR;
  2550. return inet6_dump_addr(skb, cb, type);
  2551. }
  2552. static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
  2553. {
  2554. enum addr_type_t type = ANYCAST_ADDR;
  2555. return inet6_dump_addr(skb, cb, type);
  2556. }
  2557. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  2558. {
  2559. struct sk_buff *skb;
  2560. int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
  2561. skb = alloc_skb(size, GFP_ATOMIC);
  2562. if (!skb) {
  2563. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
  2564. return;
  2565. }
  2566. if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
  2567. kfree_skb(skb);
  2568. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
  2569. return;
  2570. }
  2571. NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
  2572. netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
  2573. }
  2574. static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
  2575. __s32 *array, int bytes)
  2576. {
  2577. memset(array, 0, bytes);
  2578. array[DEVCONF_FORWARDING] = cnf->forwarding;
  2579. array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
  2580. array[DEVCONF_MTU6] = cnf->mtu6;
  2581. array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
  2582. array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
  2583. array[DEVCONF_AUTOCONF] = cnf->autoconf;
  2584. array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
  2585. array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
  2586. array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
  2587. array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
  2588. array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
  2589. #ifdef CONFIG_IPV6_PRIVACY
  2590. array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
  2591. array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
  2592. array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
  2593. array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
  2594. array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
  2595. #endif
  2596. array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
  2597. }
  2598. static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
  2599. u32 pid, u32 seq, int event, unsigned int flags)
  2600. {
  2601. struct net_device *dev = idev->dev;
  2602. __s32 *array = NULL;
  2603. struct ifinfomsg *r;
  2604. struct nlmsghdr *nlh;
  2605. unsigned char *b = skb->tail;
  2606. struct rtattr *subattr;
  2607. __u32 mtu = dev->mtu;
  2608. struct ifla_cacheinfo ci;
  2609. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
  2610. r = NLMSG_DATA(nlh);
  2611. r->ifi_family = AF_INET6;
  2612. r->__ifi_pad = 0;
  2613. r->ifi_type = dev->type;
  2614. r->ifi_index = dev->ifindex;
  2615. r->ifi_flags = dev_get_flags(dev);
  2616. r->ifi_change = 0;
  2617. RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
  2618. if (dev->addr_len)
  2619. RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  2620. RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
  2621. if (dev->ifindex != dev->iflink)
  2622. RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
  2623. subattr = (struct rtattr*)skb->tail;
  2624. RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
  2625. /* return the device flags */
  2626. RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
  2627. /* return interface cacheinfo */
  2628. ci.max_reasm_len = IPV6_MAXPLEN;
  2629. ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
  2630. + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
  2631. ci.reachable_time = idev->nd_parms->reachable_time;
  2632. ci.retrans_time = idev->nd_parms->retrans_time;
  2633. RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
  2634. /* return the device sysctl params */
  2635. if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
  2636. goto rtattr_failure;
  2637. ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
  2638. RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
  2639. /* XXX - Statistics/MC not implemented */
  2640. subattr->rta_len = skb->tail - (u8*)subattr;
  2641. nlh->nlmsg_len = skb->tail - b;
  2642. kfree(array);
  2643. return skb->len;
  2644. nlmsg_failure:
  2645. rtattr_failure:
  2646. kfree(array);
  2647. skb_trim(skb, b - skb->data);
  2648. return -1;
  2649. }
  2650. static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  2651. {
  2652. int idx, err;
  2653. int s_idx = cb->args[0];
  2654. struct net_device *dev;
  2655. struct inet6_dev *idev;
  2656. read_lock(&dev_base_lock);
  2657. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  2658. if (idx < s_idx)
  2659. continue;
  2660. if ((idev = in6_dev_get(dev)) == NULL)
  2661. continue;
  2662. err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
  2663. cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
  2664. in6_dev_put(idev);
  2665. if (err <= 0)
  2666. break;
  2667. }
  2668. read_unlock(&dev_base_lock);
  2669. cb->args[0] = idx;
  2670. return skb->len;
  2671. }
  2672. void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
  2673. {
  2674. struct sk_buff *skb;
  2675. /* 128 bytes ?? */
  2676. int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
  2677. skb = alloc_skb(size, GFP_ATOMIC);
  2678. if (!skb) {
  2679. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
  2680. return;
  2681. }
  2682. if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
  2683. kfree_skb(skb);
  2684. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
  2685. return;
  2686. }
  2687. NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
  2688. netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
  2689. }
  2690. static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
  2691. struct prefix_info *pinfo, u32 pid, u32 seq,
  2692. int event, unsigned int flags)
  2693. {
  2694. struct prefixmsg *pmsg;
  2695. struct nlmsghdr *nlh;
  2696. unsigned char *b = skb->tail;
  2697. struct prefix_cacheinfo ci;
  2698. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
  2699. pmsg = NLMSG_DATA(nlh);
  2700. pmsg->prefix_family = AF_INET6;
  2701. pmsg->prefix_pad1 = 0;
  2702. pmsg->prefix_pad2 = 0;
  2703. pmsg->prefix_ifindex = idev->dev->ifindex;
  2704. pmsg->prefix_len = pinfo->prefix_len;
  2705. pmsg->prefix_type = pinfo->type;
  2706. pmsg->prefix_pad3 = 0;
  2707. pmsg->prefix_flags = 0;
  2708. if (pinfo->onlink)
  2709. pmsg->prefix_flags |= IF_PREFIX_ONLINK;
  2710. if (pinfo->autoconf)
  2711. pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
  2712. RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
  2713. ci.preferred_time = ntohl(pinfo->prefered);
  2714. ci.valid_time = ntohl(pinfo->valid);
  2715. RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
  2716. nlh->nlmsg_len = skb->tail - b;
  2717. return skb->len;
  2718. nlmsg_failure:
  2719. rtattr_failure:
  2720. skb_trim(skb, b - skb->data);
  2721. return -1;
  2722. }
  2723. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  2724. struct prefix_info *pinfo)
  2725. {
  2726. struct sk_buff *skb;
  2727. int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
  2728. skb = alloc_skb(size, GFP_ATOMIC);
  2729. if (!skb) {
  2730. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
  2731. return;
  2732. }
  2733. if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
  2734. kfree_skb(skb);
  2735. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
  2736. return;
  2737. }
  2738. NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
  2739. netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
  2740. }
  2741. static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
  2742. [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
  2743. [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
  2744. [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
  2745. [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
  2746. [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
  2747. [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
  2748. [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
  2749. [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
  2750. [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
  2751. .dumpit = inet6_dump_fib, },
  2752. };
  2753. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  2754. {
  2755. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  2756. switch (event) {
  2757. case RTM_NEWADDR:
  2758. dst_hold(&ifp->rt->u.dst);
  2759. if (ip6_ins_rt(ifp->rt, NULL, NULL, NULL))
  2760. dst_release(&ifp->rt->u.dst);
  2761. if (ifp->idev->cnf.forwarding)
  2762. addrconf_join_anycast(ifp);
  2763. break;
  2764. case RTM_DELADDR:
  2765. if (ifp->idev->cnf.forwarding)
  2766. addrconf_leave_anycast(ifp);
  2767. addrconf_leave_solict(ifp->idev, &ifp->addr);
  2768. dst_hold(&ifp->rt->u.dst);
  2769. if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
  2770. dst_free(&ifp->rt->u.dst);
  2771. else
  2772. dst_release(&ifp->rt->u.dst);
  2773. break;
  2774. }
  2775. }
  2776. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  2777. {
  2778. read_lock_bh(&addrconf_lock);
  2779. if (likely(ifp->idev->dead == 0))
  2780. __ipv6_ifa_notify(event, ifp);
  2781. read_unlock_bh(&addrconf_lock);
  2782. }
  2783. #ifdef CONFIG_SYSCTL
  2784. static
  2785. int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
  2786. void __user *buffer, size_t *lenp, loff_t *ppos)
  2787. {
  2788. int *valp = ctl->data;
  2789. int val = *valp;
  2790. int ret;
  2791. ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  2792. if (write && valp != &ipv6_devconf_dflt.forwarding) {
  2793. if (valp != &ipv6_devconf.forwarding) {
  2794. if ((!*valp) ^ (!val)) {
  2795. struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
  2796. if (idev == NULL)
  2797. return ret;
  2798. dev_forward_change(idev);
  2799. }
  2800. } else {
  2801. ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
  2802. addrconf_forward_change();
  2803. }
  2804. if (*valp)
  2805. rt6_purge_dflt_routers();
  2806. }
  2807. return ret;
  2808. }
  2809. static int addrconf_sysctl_forward_strategy(ctl_table *table,
  2810. int __user *name, int nlen,
  2811. void __user *oldval,
  2812. size_t __user *oldlenp,
  2813. void __user *newval, size_t newlen,
  2814. void **context)
  2815. {
  2816. int *valp = table->data;
  2817. int new;
  2818. if (!newval || !newlen)
  2819. return 0;
  2820. if (newlen != sizeof(int))
  2821. return -EINVAL;
  2822. if (get_user(new, (int __user *)newval))
  2823. return -EFAULT;
  2824. if (new == *valp)
  2825. return 0;
  2826. if (oldval && oldlenp) {
  2827. size_t len;
  2828. if (get_user(len, oldlenp))
  2829. return -EFAULT;
  2830. if (len) {
  2831. if (len > table->maxlen)
  2832. len = table->maxlen;
  2833. if (copy_to_user(oldval, valp, len))
  2834. return -EFAULT;
  2835. if (put_user(len, oldlenp))
  2836. return -EFAULT;
  2837. }
  2838. }
  2839. if (valp != &ipv6_devconf_dflt.forwarding) {
  2840. if (valp != &ipv6_devconf.forwarding) {
  2841. struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
  2842. int changed;
  2843. if (unlikely(idev == NULL))
  2844. return -ENODEV;
  2845. changed = (!*valp) ^ (!new);
  2846. *valp = new;
  2847. if (changed)
  2848. dev_forward_change(idev);
  2849. } else {
  2850. *valp = new;
  2851. addrconf_forward_change();
  2852. }
  2853. if (*valp)
  2854. rt6_purge_dflt_routers();
  2855. } else
  2856. *valp = new;
  2857. return 1;
  2858. }
  2859. static struct addrconf_sysctl_table
  2860. {
  2861. struct ctl_table_header *sysctl_header;
  2862. ctl_table addrconf_vars[__NET_IPV6_MAX];
  2863. ctl_table addrconf_dev[2];
  2864. ctl_table addrconf_conf_dir[2];
  2865. ctl_table addrconf_proto_dir[2];
  2866. ctl_table addrconf_root_dir[2];
  2867. } addrconf_sysctl = {
  2868. .sysctl_header = NULL,
  2869. .addrconf_vars = {
  2870. {
  2871. .ctl_name = NET_IPV6_FORWARDING,
  2872. .procname = "forwarding",
  2873. .data = &ipv6_devconf.forwarding,
  2874. .maxlen = sizeof(int),
  2875. .mode = 0644,
  2876. .proc_handler = &addrconf_sysctl_forward,
  2877. .strategy = &addrconf_sysctl_forward_strategy,
  2878. },
  2879. {
  2880. .ctl_name = NET_IPV6_HOP_LIMIT,
  2881. .procname = "hop_limit",
  2882. .data = &ipv6_devconf.hop_limit,
  2883. .maxlen = sizeof(int),
  2884. .mode = 0644,
  2885. .proc_handler = proc_dointvec,
  2886. },
  2887. {
  2888. .ctl_name = NET_IPV6_MTU,
  2889. .procname = "mtu",
  2890. .data = &ipv6_devconf.mtu6,
  2891. .maxlen = sizeof(int),
  2892. .mode = 0644,
  2893. .proc_handler = &proc_dointvec,
  2894. },
  2895. {
  2896. .ctl_name = NET_IPV6_ACCEPT_RA,
  2897. .procname = "accept_ra",
  2898. .data = &ipv6_devconf.accept_ra,
  2899. .maxlen = sizeof(int),
  2900. .mode = 0644,
  2901. .proc_handler = &proc_dointvec,
  2902. },
  2903. {
  2904. .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
  2905. .procname = "accept_redirects",
  2906. .data = &ipv6_devconf.accept_redirects,
  2907. .maxlen = sizeof(int),
  2908. .mode = 0644,
  2909. .proc_handler = &proc_dointvec,
  2910. },
  2911. {
  2912. .ctl_name = NET_IPV6_AUTOCONF,
  2913. .procname = "autoconf",
  2914. .data = &ipv6_devconf.autoconf,
  2915. .maxlen = sizeof(int),
  2916. .mode = 0644,
  2917. .proc_handler = &proc_dointvec,
  2918. },
  2919. {
  2920. .ctl_name = NET_IPV6_DAD_TRANSMITS,
  2921. .procname = "dad_transmits",
  2922. .data = &ipv6_devconf.dad_transmits,
  2923. .maxlen = sizeof(int),
  2924. .mode = 0644,
  2925. .proc_handler = &proc_dointvec,
  2926. },
  2927. {
  2928. .ctl_name = NET_IPV6_RTR_SOLICITS,
  2929. .procname = "router_solicitations",
  2930. .data = &ipv6_devconf.rtr_solicits,
  2931. .maxlen = sizeof(int),
  2932. .mode = 0644,
  2933. .proc_handler = &proc_dointvec,
  2934. },
  2935. {
  2936. .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
  2937. .procname = "router_solicitation_interval",
  2938. .data = &ipv6_devconf.rtr_solicit_interval,
  2939. .maxlen = sizeof(int),
  2940. .mode = 0644,
  2941. .proc_handler = &proc_dointvec_jiffies,
  2942. .strategy = &sysctl_jiffies,
  2943. },
  2944. {
  2945. .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
  2946. .procname = "router_solicitation_delay",
  2947. .data = &ipv6_devconf.rtr_solicit_delay,
  2948. .maxlen = sizeof(int),
  2949. .mode = 0644,
  2950. .proc_handler = &proc_dointvec_jiffies,
  2951. .strategy = &sysctl_jiffies,
  2952. },
  2953. {
  2954. .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
  2955. .procname = "force_mld_version",
  2956. .data = &ipv6_devconf.force_mld_version,
  2957. .maxlen = sizeof(int),
  2958. .mode = 0644,
  2959. .proc_handler = &proc_dointvec,
  2960. },
  2961. #ifdef CONFIG_IPV6_PRIVACY
  2962. {
  2963. .ctl_name = NET_IPV6_USE_TEMPADDR,
  2964. .procname = "use_tempaddr",
  2965. .data = &ipv6_devconf.use_tempaddr,
  2966. .maxlen = sizeof(int),
  2967. .mode = 0644,
  2968. .proc_handler = &proc_dointvec,
  2969. },
  2970. {
  2971. .ctl_name = NET_IPV6_TEMP_VALID_LFT,
  2972. .procname = "temp_valid_lft",
  2973. .data = &ipv6_devconf.temp_valid_lft,
  2974. .maxlen = sizeof(int),
  2975. .mode = 0644,
  2976. .proc_handler = &proc_dointvec,
  2977. },
  2978. {
  2979. .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
  2980. .procname = "temp_prefered_lft",
  2981. .data = &ipv6_devconf.temp_prefered_lft,
  2982. .maxlen = sizeof(int),
  2983. .mode = 0644,
  2984. .proc_handler = &proc_dointvec,
  2985. },
  2986. {
  2987. .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
  2988. .procname = "regen_max_retry",
  2989. .data = &ipv6_devconf.regen_max_retry,
  2990. .maxlen = sizeof(int),
  2991. .mode = 0644,
  2992. .proc_handler = &proc_dointvec,
  2993. },
  2994. {
  2995. .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
  2996. .procname = "max_desync_factor",
  2997. .data = &ipv6_devconf.max_desync_factor,
  2998. .maxlen = sizeof(int),
  2999. .mode = 0644,
  3000. .proc_handler = &proc_dointvec,
  3001. },
  3002. #endif
  3003. {
  3004. .ctl_name = NET_IPV6_MAX_ADDRESSES,
  3005. .procname = "max_addresses",
  3006. .data = &ipv6_devconf.max_addresses,
  3007. .maxlen = sizeof(int),
  3008. .mode = 0644,
  3009. .proc_handler = &proc_dointvec,
  3010. },
  3011. {
  3012. .ctl_name = 0, /* sentinel */
  3013. }
  3014. },
  3015. .addrconf_dev = {
  3016. {
  3017. .ctl_name = NET_PROTO_CONF_ALL,
  3018. .procname = "all",
  3019. .mode = 0555,
  3020. .child = addrconf_sysctl.addrconf_vars,
  3021. },
  3022. {
  3023. .ctl_name = 0, /* sentinel */
  3024. }
  3025. },
  3026. .addrconf_conf_dir = {
  3027. {
  3028. .ctl_name = NET_IPV6_CONF,
  3029. .procname = "conf",
  3030. .mode = 0555,
  3031. .child = addrconf_sysctl.addrconf_dev,
  3032. },
  3033. {
  3034. .ctl_name = 0, /* sentinel */
  3035. }
  3036. },
  3037. .addrconf_proto_dir = {
  3038. {
  3039. .ctl_name = NET_IPV6,
  3040. .procname = "ipv6",
  3041. .mode = 0555,
  3042. .child = addrconf_sysctl.addrconf_conf_dir,
  3043. },
  3044. {
  3045. .ctl_name = 0, /* sentinel */
  3046. }
  3047. },
  3048. .addrconf_root_dir = {
  3049. {
  3050. .ctl_name = CTL_NET,
  3051. .procname = "net",
  3052. .mode = 0555,
  3053. .child = addrconf_sysctl.addrconf_proto_dir,
  3054. },
  3055. {
  3056. .ctl_name = 0, /* sentinel */
  3057. }
  3058. },
  3059. };
  3060. static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
  3061. {
  3062. int i;
  3063. struct net_device *dev = idev ? idev->dev : NULL;
  3064. struct addrconf_sysctl_table *t;
  3065. char *dev_name = NULL;
  3066. t = kmalloc(sizeof(*t), GFP_KERNEL);
  3067. if (t == NULL)
  3068. return;
  3069. memcpy(t, &addrconf_sysctl, sizeof(*t));
  3070. for (i=0; t->addrconf_vars[i].data; i++) {
  3071. t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
  3072. t->addrconf_vars[i].de = NULL;
  3073. t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
  3074. }
  3075. if (dev) {
  3076. dev_name = dev->name;
  3077. t->addrconf_dev[0].ctl_name = dev->ifindex;
  3078. } else {
  3079. dev_name = "default";
  3080. t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
  3081. }
  3082. /*
  3083. * Make a copy of dev_name, because '.procname' is regarded as const
  3084. * by sysctl and we wouldn't want anyone to change it under our feet
  3085. * (see SIOCSIFNAME).
  3086. */
  3087. dev_name = kstrdup(dev_name, GFP_KERNEL);
  3088. if (!dev_name)
  3089. goto free;
  3090. t->addrconf_dev[0].procname = dev_name;
  3091. t->addrconf_dev[0].child = t->addrconf_vars;
  3092. t->addrconf_dev[0].de = NULL;
  3093. t->addrconf_conf_dir[0].child = t->addrconf_dev;
  3094. t->addrconf_conf_dir[0].de = NULL;
  3095. t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
  3096. t->addrconf_proto_dir[0].de = NULL;
  3097. t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
  3098. t->addrconf_root_dir[0].de = NULL;
  3099. t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
  3100. if (t->sysctl_header == NULL)
  3101. goto free_procname;
  3102. else
  3103. p->sysctl = t;
  3104. return;
  3105. /* error path */
  3106. free_procname:
  3107. kfree(dev_name);
  3108. free:
  3109. kfree(t);
  3110. return;
  3111. }
  3112. static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
  3113. {
  3114. if (p->sysctl) {
  3115. struct addrconf_sysctl_table *t = p->sysctl;
  3116. p->sysctl = NULL;
  3117. unregister_sysctl_table(t->sysctl_header);
  3118. kfree(t->addrconf_dev[0].procname);
  3119. kfree(t);
  3120. }
  3121. }
  3122. #endif
  3123. /*
  3124. * Device notifier
  3125. */
  3126. int register_inet6addr_notifier(struct notifier_block *nb)
  3127. {
  3128. return notifier_chain_register(&inet6addr_chain, nb);
  3129. }
  3130. int unregister_inet6addr_notifier(struct notifier_block *nb)
  3131. {
  3132. return notifier_chain_unregister(&inet6addr_chain,nb);
  3133. }
  3134. /*
  3135. * Init / cleanup code
  3136. */
  3137. int __init addrconf_init(void)
  3138. {
  3139. int err = 0;
  3140. /* The addrconf netdev notifier requires that loopback_dev
  3141. * has it's ipv6 private information allocated and setup
  3142. * before it can bring up and give link-local addresses
  3143. * to other devices which are up.
  3144. *
  3145. * Unfortunately, loopback_dev is not necessarily the first
  3146. * entry in the global dev_base list of net devices. In fact,
  3147. * it is likely to be the very last entry on that list.
  3148. * So this causes the notifier registry below to try and
  3149. * give link-local addresses to all devices besides loopback_dev
  3150. * first, then loopback_dev, which cases all the non-loopback_dev
  3151. * devices to fail to get a link-local address.
  3152. *
  3153. * So, as a temporary fix, allocate the ipv6 structure for
  3154. * loopback_dev first by hand.
  3155. * Longer term, all of the dependencies ipv6 has upon the loopback
  3156. * device and it being up should be removed.
  3157. */
  3158. rtnl_lock();
  3159. if (!ipv6_add_dev(&loopback_dev))
  3160. err = -ENOMEM;
  3161. rtnl_unlock();
  3162. if (err)
  3163. return err;
  3164. ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
  3165. register_netdevice_notifier(&ipv6_dev_notf);
  3166. #ifdef CONFIG_IPV6_PRIVACY
  3167. md5_tfm = crypto_alloc_tfm("md5", 0);
  3168. if (unlikely(md5_tfm == NULL))
  3169. printk(KERN_WARNING
  3170. "failed to load transform for md5\n");
  3171. #endif
  3172. addrconf_verify(0);
  3173. rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
  3174. #ifdef CONFIG_SYSCTL
  3175. addrconf_sysctl.sysctl_header =
  3176. register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
  3177. addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
  3178. #endif
  3179. return 0;
  3180. }
  3181. void __exit addrconf_cleanup(void)
  3182. {
  3183. struct net_device *dev;
  3184. struct inet6_dev *idev;
  3185. struct inet6_ifaddr *ifa;
  3186. int i;
  3187. unregister_netdevice_notifier(&ipv6_dev_notf);
  3188. rtnetlink_links[PF_INET6] = NULL;
  3189. #ifdef CONFIG_SYSCTL
  3190. addrconf_sysctl_unregister(&ipv6_devconf_dflt);
  3191. addrconf_sysctl_unregister(&ipv6_devconf);
  3192. #endif
  3193. rtnl_lock();
  3194. /*
  3195. * clean dev list.
  3196. */
  3197. for (dev=dev_base; dev; dev=dev->next) {
  3198. if ((idev = __in6_dev_get(dev)) == NULL)
  3199. continue;
  3200. addrconf_ifdown(dev, 1);
  3201. }
  3202. addrconf_ifdown(&loopback_dev, 2);
  3203. /*
  3204. * Check hash table.
  3205. */
  3206. write_lock_bh(&addrconf_hash_lock);
  3207. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  3208. for (ifa=inet6_addr_lst[i]; ifa; ) {
  3209. struct inet6_ifaddr *bifa;
  3210. bifa = ifa;
  3211. ifa = ifa->lst_next;
  3212. printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
  3213. /* Do not free it; something is wrong.
  3214. Now we can investigate it with debugger.
  3215. */
  3216. }
  3217. }
  3218. write_unlock_bh(&addrconf_hash_lock);
  3219. del_timer(&addr_chk_timer);
  3220. rtnl_unlock();
  3221. #ifdef CONFIG_IPV6_PRIVACY
  3222. crypto_free_tfm(md5_tfm);
  3223. md5_tfm = NULL;
  3224. #endif
  3225. #ifdef CONFIG_PROC_FS
  3226. proc_net_remove("if_inet6");
  3227. #endif
  3228. }