addrconf.c 93 KB

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