addrconf.c 87 KB

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