addrconf.c 92 KB

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