addrconf.c 99 KB

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