addrconf.c 108 KB

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