addrconf.c 95 KB

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