addrconf.c 101 KB

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