addrconf.c 87 KB

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