addrconf.c 93 KB

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