addrconf.c 96 KB

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