addrconf.c 100 KB

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