addrconf.c 94 KB

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