addrconf.c 94 KB

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