addrconf.c 95 KB

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