addrconf.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105
  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. if (netif_carrier_ok(dev))
  303. ndev->if_flags |= IF_READY;
  304. ipv6_mc_init_dev(ndev);
  305. ndev->tstamp = jiffies;
  306. #ifdef CONFIG_SYSCTL
  307. neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
  308. NET_IPV6_NEIGH, "ipv6",
  309. &ndisc_ifinfo_sysctl_change,
  310. NULL);
  311. addrconf_sysctl_register(ndev, &ndev->cnf);
  312. #endif
  313. /* protected by rtnl_lock */
  314. rcu_assign_pointer(dev->ip6_ptr, ndev);
  315. /* Join all-node multicast group */
  316. ipv6_addr_all_nodes(&maddr);
  317. ipv6_dev_mc_inc(dev, &maddr);
  318. return ndev;
  319. }
  320. static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
  321. {
  322. struct inet6_dev *idev;
  323. ASSERT_RTNL();
  324. if ((idev = __in6_dev_get(dev)) == NULL) {
  325. if ((idev = ipv6_add_dev(dev)) == NULL)
  326. return NULL;
  327. }
  328. if (dev->flags&IFF_UP)
  329. ipv6_mc_up(idev);
  330. return idev;
  331. }
  332. #ifdef CONFIG_SYSCTL
  333. static void dev_forward_change(struct inet6_dev *idev)
  334. {
  335. struct net_device *dev;
  336. struct inet6_ifaddr *ifa;
  337. struct in6_addr addr;
  338. if (!idev)
  339. return;
  340. dev = idev->dev;
  341. if (dev && (dev->flags & IFF_MULTICAST)) {
  342. ipv6_addr_all_routers(&addr);
  343. if (idev->cnf.forwarding)
  344. ipv6_dev_mc_inc(dev, &addr);
  345. else
  346. ipv6_dev_mc_dec(dev, &addr);
  347. }
  348. for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
  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. ASSERT_RTNL();
  1602. /* check the lifetime */
  1603. if (!valid_lft || prefered_lft > valid_lft)
  1604. return -EINVAL;
  1605. if ((dev = __dev_get_by_index(ifindex)) == NULL)
  1606. return -ENODEV;
  1607. if ((idev = addrconf_add_dev(dev)) == NULL)
  1608. return -ENOBUFS;
  1609. scope = ipv6_addr_scope(pfx);
  1610. if (valid_lft == INFINITY_LIFE_TIME)
  1611. ifa_flags |= IFA_F_PERMANENT;
  1612. else if (valid_lft >= 0x7FFFFFFF/HZ)
  1613. valid_lft = 0x7FFFFFFF/HZ;
  1614. if (prefered_lft == 0)
  1615. ifa_flags |= IFA_F_DEPRECATED;
  1616. else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
  1617. (prefered_lft != INFINITY_LIFE_TIME))
  1618. prefered_lft = 0x7FFFFFFF/HZ;
  1619. ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
  1620. if (!IS_ERR(ifp)) {
  1621. spin_lock_bh(&ifp->lock);
  1622. ifp->valid_lft = valid_lft;
  1623. ifp->prefered_lft = prefered_lft;
  1624. ifp->tstamp = jiffies;
  1625. spin_unlock_bh(&ifp->lock);
  1626. addrconf_dad_start(ifp, 0);
  1627. in6_ifa_put(ifp);
  1628. addrconf_verify(0);
  1629. return 0;
  1630. }
  1631. return PTR_ERR(ifp);
  1632. }
  1633. static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
  1634. {
  1635. struct inet6_ifaddr *ifp;
  1636. struct inet6_dev *idev;
  1637. struct net_device *dev;
  1638. if ((dev = __dev_get_by_index(ifindex)) == NULL)
  1639. return -ENODEV;
  1640. if ((idev = __in6_dev_get(dev)) == NULL)
  1641. return -ENXIO;
  1642. read_lock_bh(&idev->lock);
  1643. for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
  1644. if (ifp->prefix_len == plen &&
  1645. ipv6_addr_equal(pfx, &ifp->addr)) {
  1646. in6_ifa_hold(ifp);
  1647. read_unlock_bh(&idev->lock);
  1648. ipv6_del_addr(ifp);
  1649. /* If the last address is deleted administratively,
  1650. disable IPv6 on this interface.
  1651. */
  1652. if (idev->addr_list == NULL)
  1653. addrconf_ifdown(idev->dev, 1);
  1654. return 0;
  1655. }
  1656. }
  1657. read_unlock_bh(&idev->lock);
  1658. return -EADDRNOTAVAIL;
  1659. }
  1660. int addrconf_add_ifaddr(void __user *arg)
  1661. {
  1662. struct in6_ifreq ireq;
  1663. int err;
  1664. if (!capable(CAP_NET_ADMIN))
  1665. return -EPERM;
  1666. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  1667. return -EFAULT;
  1668. rtnl_lock();
  1669. err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
  1670. IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
  1671. rtnl_unlock();
  1672. return err;
  1673. }
  1674. int addrconf_del_ifaddr(void __user *arg)
  1675. {
  1676. struct in6_ifreq ireq;
  1677. int err;
  1678. if (!capable(CAP_NET_ADMIN))
  1679. return -EPERM;
  1680. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  1681. return -EFAULT;
  1682. rtnl_lock();
  1683. err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
  1684. rtnl_unlock();
  1685. return err;
  1686. }
  1687. #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
  1688. static void sit_add_v4_addrs(struct inet6_dev *idev)
  1689. {
  1690. struct inet6_ifaddr * ifp;
  1691. struct in6_addr addr;
  1692. struct net_device *dev;
  1693. int scope;
  1694. ASSERT_RTNL();
  1695. memset(&addr, 0, sizeof(struct in6_addr));
  1696. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  1697. if (idev->dev->flags&IFF_POINTOPOINT) {
  1698. addr.s6_addr32[0] = htonl(0xfe800000);
  1699. scope = IFA_LINK;
  1700. } else {
  1701. scope = IPV6_ADDR_COMPATv4;
  1702. }
  1703. if (addr.s6_addr32[3]) {
  1704. ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
  1705. if (!IS_ERR(ifp)) {
  1706. spin_lock_bh(&ifp->lock);
  1707. ifp->flags &= ~IFA_F_TENTATIVE;
  1708. spin_unlock_bh(&ifp->lock);
  1709. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1710. in6_ifa_put(ifp);
  1711. }
  1712. return;
  1713. }
  1714. for (dev = dev_base; dev != NULL; dev = dev->next) {
  1715. struct in_device * in_dev = __in_dev_get_rtnl(dev);
  1716. if (in_dev && (dev->flags & IFF_UP)) {
  1717. struct in_ifaddr * ifa;
  1718. int flag = scope;
  1719. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  1720. int plen;
  1721. addr.s6_addr32[3] = ifa->ifa_local;
  1722. if (ifa->ifa_scope == RT_SCOPE_LINK)
  1723. continue;
  1724. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  1725. if (idev->dev->flags&IFF_POINTOPOINT)
  1726. continue;
  1727. flag |= IFA_HOST;
  1728. }
  1729. if (idev->dev->flags&IFF_POINTOPOINT)
  1730. plen = 64;
  1731. else
  1732. plen = 96;
  1733. ifp = ipv6_add_addr(idev, &addr, plen, flag,
  1734. IFA_F_PERMANENT);
  1735. if (!IS_ERR(ifp)) {
  1736. spin_lock_bh(&ifp->lock);
  1737. ifp->flags &= ~IFA_F_TENTATIVE;
  1738. spin_unlock_bh(&ifp->lock);
  1739. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1740. in6_ifa_put(ifp);
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. #endif
  1747. static void init_loopback(struct net_device *dev)
  1748. {
  1749. struct inet6_dev *idev;
  1750. struct inet6_ifaddr * ifp;
  1751. /* ::1 */
  1752. ASSERT_RTNL();
  1753. if ((idev = ipv6_find_idev(dev)) == NULL) {
  1754. printk(KERN_DEBUG "init loopback: add_dev failed\n");
  1755. return;
  1756. }
  1757. ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
  1758. if (!IS_ERR(ifp)) {
  1759. spin_lock_bh(&ifp->lock);
  1760. ifp->flags &= ~IFA_F_TENTATIVE;
  1761. spin_unlock_bh(&ifp->lock);
  1762. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1763. in6_ifa_put(ifp);
  1764. }
  1765. }
  1766. static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
  1767. {
  1768. struct inet6_ifaddr * ifp;
  1769. ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
  1770. if (!IS_ERR(ifp)) {
  1771. addrconf_dad_start(ifp, 0);
  1772. in6_ifa_put(ifp);
  1773. }
  1774. }
  1775. static void addrconf_dev_config(struct net_device *dev)
  1776. {
  1777. struct in6_addr addr;
  1778. struct inet6_dev * idev;
  1779. ASSERT_RTNL();
  1780. if ((dev->type != ARPHRD_ETHER) &&
  1781. (dev->type != ARPHRD_FDDI) &&
  1782. (dev->type != ARPHRD_IEEE802_TR) &&
  1783. (dev->type != ARPHRD_ARCNET) &&
  1784. (dev->type != ARPHRD_INFINIBAND)) {
  1785. /* Alas, we support only Ethernet autoconfiguration. */
  1786. return;
  1787. }
  1788. idev = addrconf_add_dev(dev);
  1789. if (idev == NULL)
  1790. return;
  1791. memset(&addr, 0, sizeof(struct in6_addr));
  1792. addr.s6_addr32[0] = htonl(0xFE800000);
  1793. if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
  1794. addrconf_add_linklocal(idev, &addr);
  1795. }
  1796. #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
  1797. static void addrconf_sit_config(struct net_device *dev)
  1798. {
  1799. struct inet6_dev *idev;
  1800. ASSERT_RTNL();
  1801. /*
  1802. * Configure the tunnel with one of our IPv4
  1803. * addresses... we should configure all of
  1804. * our v4 addrs in the tunnel
  1805. */
  1806. if ((idev = ipv6_find_idev(dev)) == NULL) {
  1807. printk(KERN_DEBUG "init sit: add_dev failed\n");
  1808. return;
  1809. }
  1810. sit_add_v4_addrs(idev);
  1811. if (dev->flags&IFF_POINTOPOINT) {
  1812. addrconf_add_mroute(dev);
  1813. addrconf_add_lroute(dev);
  1814. } else
  1815. sit_route_add(dev);
  1816. }
  1817. #endif
  1818. static inline int
  1819. ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
  1820. {
  1821. struct in6_addr lladdr;
  1822. if (!ipv6_get_lladdr(link_dev, &lladdr)) {
  1823. addrconf_add_linklocal(idev, &lladdr);
  1824. return 0;
  1825. }
  1826. return -1;
  1827. }
  1828. static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
  1829. {
  1830. struct net_device *link_dev;
  1831. /* first try to inherit the link-local address from the link device */
  1832. if (idev->dev->iflink &&
  1833. (link_dev = __dev_get_by_index(idev->dev->iflink))) {
  1834. if (!ipv6_inherit_linklocal(idev, link_dev))
  1835. return;
  1836. }
  1837. /* then try to inherit it from any device */
  1838. for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
  1839. if (!ipv6_inherit_linklocal(idev, link_dev))
  1840. return;
  1841. }
  1842. printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
  1843. }
  1844. /*
  1845. * Autoconfigure tunnel with a link-local address so routing protocols,
  1846. * DHCPv6, MLD etc. can be run over the virtual link
  1847. */
  1848. static void addrconf_ip6_tnl_config(struct net_device *dev)
  1849. {
  1850. struct inet6_dev *idev;
  1851. ASSERT_RTNL();
  1852. if ((idev = addrconf_add_dev(dev)) == NULL) {
  1853. printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
  1854. return;
  1855. }
  1856. ip6_tnl_add_linklocal(idev);
  1857. }
  1858. static int addrconf_notify(struct notifier_block *this, unsigned long event,
  1859. void * data)
  1860. {
  1861. struct net_device *dev = (struct net_device *) data;
  1862. struct inet6_dev *idev = __in6_dev_get(dev);
  1863. int run_pending = 0;
  1864. switch(event) {
  1865. case NETDEV_UP:
  1866. case NETDEV_CHANGE:
  1867. if (event == NETDEV_UP) {
  1868. if (!netif_carrier_ok(dev)) {
  1869. /* device is not ready yet. */
  1870. printk(KERN_INFO
  1871. "ADDRCONF(NETDEV_UP): %s: "
  1872. "link is not ready\n",
  1873. dev->name);
  1874. break;
  1875. }
  1876. if (idev)
  1877. idev->if_flags |= IF_READY;
  1878. } else {
  1879. if (!netif_carrier_ok(dev)) {
  1880. /* device is still not ready. */
  1881. break;
  1882. }
  1883. if (idev) {
  1884. if (idev->if_flags & IF_READY) {
  1885. /* device is already configured. */
  1886. break;
  1887. }
  1888. idev->if_flags |= IF_READY;
  1889. }
  1890. printk(KERN_INFO
  1891. "ADDRCONF(NETDEV_CHANGE): %s: "
  1892. "link becomes ready\n",
  1893. dev->name);
  1894. run_pending = 1;
  1895. }
  1896. switch(dev->type) {
  1897. #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
  1898. case ARPHRD_SIT:
  1899. addrconf_sit_config(dev);
  1900. break;
  1901. #endif
  1902. case ARPHRD_TUNNEL6:
  1903. addrconf_ip6_tnl_config(dev);
  1904. break;
  1905. case ARPHRD_LOOPBACK:
  1906. init_loopback(dev);
  1907. break;
  1908. default:
  1909. addrconf_dev_config(dev);
  1910. break;
  1911. };
  1912. if (idev) {
  1913. if (run_pending)
  1914. addrconf_dad_run(idev);
  1915. /* If the MTU changed during the interface down, when the
  1916. interface up, the changed MTU must be reflected in the
  1917. idev as well as routers.
  1918. */
  1919. if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
  1920. rt6_mtu_change(dev, dev->mtu);
  1921. idev->cnf.mtu6 = dev->mtu;
  1922. }
  1923. idev->tstamp = jiffies;
  1924. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1925. /* If the changed mtu during down is lower than IPV6_MIN_MTU
  1926. stop IPv6 on this interface.
  1927. */
  1928. if (dev->mtu < IPV6_MIN_MTU)
  1929. addrconf_ifdown(dev, event != NETDEV_DOWN);
  1930. }
  1931. break;
  1932. case NETDEV_CHANGEMTU:
  1933. if ( idev && dev->mtu >= IPV6_MIN_MTU) {
  1934. rt6_mtu_change(dev, dev->mtu);
  1935. idev->cnf.mtu6 = dev->mtu;
  1936. break;
  1937. }
  1938. /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
  1939. case NETDEV_DOWN:
  1940. case NETDEV_UNREGISTER:
  1941. /*
  1942. * Remove all addresses from this interface.
  1943. */
  1944. addrconf_ifdown(dev, event != NETDEV_DOWN);
  1945. break;
  1946. case NETDEV_CHANGENAME:
  1947. #ifdef CONFIG_SYSCTL
  1948. if (idev) {
  1949. addrconf_sysctl_unregister(&idev->cnf);
  1950. neigh_sysctl_unregister(idev->nd_parms);
  1951. neigh_sysctl_register(dev, idev->nd_parms,
  1952. NET_IPV6, NET_IPV6_NEIGH, "ipv6",
  1953. &ndisc_ifinfo_sysctl_change,
  1954. NULL);
  1955. addrconf_sysctl_register(idev, &idev->cnf);
  1956. }
  1957. #endif
  1958. break;
  1959. };
  1960. return NOTIFY_OK;
  1961. }
  1962. /*
  1963. * addrconf module should be notified of a device going up
  1964. */
  1965. static struct notifier_block ipv6_dev_notf = {
  1966. .notifier_call = addrconf_notify,
  1967. .priority = 0
  1968. };
  1969. static int addrconf_ifdown(struct net_device *dev, int how)
  1970. {
  1971. struct inet6_dev *idev;
  1972. struct inet6_ifaddr *ifa, **bifa;
  1973. int i;
  1974. ASSERT_RTNL();
  1975. if (dev == &loopback_dev && how == 1)
  1976. how = 0;
  1977. rt6_ifdown(dev);
  1978. neigh_ifdown(&nd_tbl, dev);
  1979. idev = __in6_dev_get(dev);
  1980. if (idev == NULL)
  1981. return -ENODEV;
  1982. /* Step 1: remove reference to ipv6 device from parent device.
  1983. Do not dev_put!
  1984. */
  1985. if (how == 1) {
  1986. idev->dead = 1;
  1987. /* protected by rtnl_lock */
  1988. rcu_assign_pointer(dev->ip6_ptr, NULL);
  1989. /* Step 1.5: remove snmp6 entry */
  1990. snmp6_unregister_dev(idev);
  1991. }
  1992. /* Step 2: clear hash table */
  1993. for (i=0; i<IN6_ADDR_HSIZE; i++) {
  1994. bifa = &inet6_addr_lst[i];
  1995. write_lock_bh(&addrconf_hash_lock);
  1996. while ((ifa = *bifa) != NULL) {
  1997. if (ifa->idev == idev) {
  1998. *bifa = ifa->lst_next;
  1999. ifa->lst_next = NULL;
  2000. addrconf_del_timer(ifa);
  2001. in6_ifa_put(ifa);
  2002. continue;
  2003. }
  2004. bifa = &ifa->lst_next;
  2005. }
  2006. write_unlock_bh(&addrconf_hash_lock);
  2007. }
  2008. write_lock_bh(&idev->lock);
  2009. /* Step 3: clear flags for stateless addrconf */
  2010. if (how != 1)
  2011. idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
  2012. /* Step 4: clear address list */
  2013. #ifdef CONFIG_IPV6_PRIVACY
  2014. if (how == 1 && del_timer(&idev->regen_timer))
  2015. in6_dev_put(idev);
  2016. /* clear tempaddr list */
  2017. while ((ifa = idev->tempaddr_list) != NULL) {
  2018. idev->tempaddr_list = ifa->tmp_next;
  2019. ifa->tmp_next = NULL;
  2020. ifa->dead = 1;
  2021. write_unlock_bh(&idev->lock);
  2022. spin_lock_bh(&ifa->lock);
  2023. if (ifa->ifpub) {
  2024. in6_ifa_put(ifa->ifpub);
  2025. ifa->ifpub = NULL;
  2026. }
  2027. spin_unlock_bh(&ifa->lock);
  2028. in6_ifa_put(ifa);
  2029. write_lock_bh(&idev->lock);
  2030. }
  2031. #endif
  2032. while ((ifa = idev->addr_list) != NULL) {
  2033. idev->addr_list = ifa->if_next;
  2034. ifa->if_next = NULL;
  2035. ifa->dead = 1;
  2036. addrconf_del_timer(ifa);
  2037. write_unlock_bh(&idev->lock);
  2038. __ipv6_ifa_notify(RTM_DELADDR, ifa);
  2039. in6_ifa_put(ifa);
  2040. write_lock_bh(&idev->lock);
  2041. }
  2042. write_unlock_bh(&idev->lock);
  2043. /* Step 5: Discard multicast list */
  2044. if (how == 1)
  2045. ipv6_mc_destroy_dev(idev);
  2046. else
  2047. ipv6_mc_down(idev);
  2048. /* Step 5: netlink notification of this interface */
  2049. idev->tstamp = jiffies;
  2050. inet6_ifinfo_notify(RTM_DELLINK, idev);
  2051. /* Shot the device (if unregistered) */
  2052. if (how == 1) {
  2053. #ifdef CONFIG_SYSCTL
  2054. addrconf_sysctl_unregister(&idev->cnf);
  2055. neigh_sysctl_unregister(idev->nd_parms);
  2056. #endif
  2057. neigh_parms_release(&nd_tbl, idev->nd_parms);
  2058. neigh_ifdown(&nd_tbl, dev);
  2059. in6_dev_put(idev);
  2060. }
  2061. return 0;
  2062. }
  2063. static void addrconf_rs_timer(unsigned long data)
  2064. {
  2065. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  2066. if (ifp->idev->cnf.forwarding)
  2067. goto out;
  2068. if (ifp->idev->if_flags & IF_RA_RCVD) {
  2069. /*
  2070. * Announcement received after solicitation
  2071. * was sent
  2072. */
  2073. goto out;
  2074. }
  2075. spin_lock(&ifp->lock);
  2076. if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
  2077. struct in6_addr all_routers;
  2078. /* The wait after the last probe can be shorter */
  2079. addrconf_mod_timer(ifp, AC_RS,
  2080. (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
  2081. ifp->idev->cnf.rtr_solicit_delay :
  2082. ifp->idev->cnf.rtr_solicit_interval);
  2083. spin_unlock(&ifp->lock);
  2084. ipv6_addr_all_routers(&all_routers);
  2085. ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
  2086. } else {
  2087. spin_unlock(&ifp->lock);
  2088. /*
  2089. * Note: we do not support deprecated "all on-link"
  2090. * assumption any longer.
  2091. */
  2092. printk(KERN_DEBUG "%s: no IPv6 routers present\n",
  2093. ifp->idev->dev->name);
  2094. }
  2095. out:
  2096. in6_ifa_put(ifp);
  2097. }
  2098. /*
  2099. * Duplicate Address Detection
  2100. */
  2101. static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
  2102. {
  2103. unsigned long rand_num;
  2104. struct inet6_dev *idev = ifp->idev;
  2105. rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
  2106. ifp->probes = idev->cnf.dad_transmits;
  2107. addrconf_mod_timer(ifp, AC_DAD, rand_num);
  2108. }
  2109. static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
  2110. {
  2111. struct inet6_dev *idev = ifp->idev;
  2112. struct net_device *dev = idev->dev;
  2113. addrconf_join_solict(dev, &ifp->addr);
  2114. if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
  2115. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
  2116. flags);
  2117. net_srandom(ifp->addr.s6_addr32[3]);
  2118. read_lock_bh(&idev->lock);
  2119. if (ifp->dead)
  2120. goto out;
  2121. spin_lock_bh(&ifp->lock);
  2122. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  2123. !(ifp->flags&IFA_F_TENTATIVE) ||
  2124. ifp->flags & IFA_F_NODAD) {
  2125. ifp->flags &= ~IFA_F_TENTATIVE;
  2126. spin_unlock_bh(&ifp->lock);
  2127. read_unlock_bh(&idev->lock);
  2128. addrconf_dad_completed(ifp);
  2129. return;
  2130. }
  2131. if (!(idev->if_flags & IF_READY)) {
  2132. spin_unlock_bh(&ifp->lock);
  2133. read_unlock_bh(&idev->lock);
  2134. /*
  2135. * If the defice is not ready:
  2136. * - keep it tentative if it is a permanent address.
  2137. * - otherwise, kill it.
  2138. */
  2139. in6_ifa_hold(ifp);
  2140. addrconf_dad_stop(ifp);
  2141. return;
  2142. }
  2143. addrconf_dad_kick(ifp);
  2144. spin_unlock_bh(&ifp->lock);
  2145. out:
  2146. read_unlock_bh(&idev->lock);
  2147. }
  2148. static void addrconf_dad_timer(unsigned long data)
  2149. {
  2150. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  2151. struct inet6_dev *idev = ifp->idev;
  2152. struct in6_addr unspec;
  2153. struct in6_addr mcaddr;
  2154. read_lock_bh(&idev->lock);
  2155. if (idev->dead) {
  2156. read_unlock_bh(&idev->lock);
  2157. goto out;
  2158. }
  2159. spin_lock_bh(&ifp->lock);
  2160. if (ifp->probes == 0) {
  2161. /*
  2162. * DAD was successful
  2163. */
  2164. ifp->flags &= ~IFA_F_TENTATIVE;
  2165. spin_unlock_bh(&ifp->lock);
  2166. read_unlock_bh(&idev->lock);
  2167. addrconf_dad_completed(ifp);
  2168. goto out;
  2169. }
  2170. ifp->probes--;
  2171. addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
  2172. spin_unlock_bh(&ifp->lock);
  2173. read_unlock_bh(&idev->lock);
  2174. /* send a neighbour solicitation for our addr */
  2175. memset(&unspec, 0, sizeof(unspec));
  2176. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  2177. ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
  2178. out:
  2179. in6_ifa_put(ifp);
  2180. }
  2181. static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
  2182. {
  2183. struct net_device * dev = ifp->idev->dev;
  2184. /*
  2185. * Configure the address for reception. Now it is valid.
  2186. */
  2187. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2188. /* If added prefix is link local and forwarding is off,
  2189. start sending router solicitations.
  2190. */
  2191. if (ifp->idev->cnf.forwarding == 0 &&
  2192. ifp->idev->cnf.rtr_solicits > 0 &&
  2193. (dev->flags&IFF_LOOPBACK) == 0 &&
  2194. (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
  2195. struct in6_addr all_routers;
  2196. ipv6_addr_all_routers(&all_routers);
  2197. /*
  2198. * If a host as already performed a random delay
  2199. * [...] as part of DAD [...] there is no need
  2200. * to delay again before sending the first RS
  2201. */
  2202. ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
  2203. spin_lock_bh(&ifp->lock);
  2204. ifp->probes = 1;
  2205. ifp->idev->if_flags |= IF_RS_SENT;
  2206. addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
  2207. spin_unlock_bh(&ifp->lock);
  2208. }
  2209. }
  2210. static void addrconf_dad_run(struct inet6_dev *idev) {
  2211. struct inet6_ifaddr *ifp;
  2212. read_lock_bh(&idev->lock);
  2213. for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
  2214. spin_lock_bh(&ifp->lock);
  2215. if (!(ifp->flags & IFA_F_TENTATIVE)) {
  2216. spin_unlock_bh(&ifp->lock);
  2217. continue;
  2218. }
  2219. spin_unlock_bh(&ifp->lock);
  2220. addrconf_dad_kick(ifp);
  2221. }
  2222. read_unlock_bh(&idev->lock);
  2223. }
  2224. #ifdef CONFIG_PROC_FS
  2225. struct if6_iter_state {
  2226. int bucket;
  2227. };
  2228. static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
  2229. {
  2230. struct inet6_ifaddr *ifa = NULL;
  2231. struct if6_iter_state *state = seq->private;
  2232. for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
  2233. ifa = inet6_addr_lst[state->bucket];
  2234. if (ifa)
  2235. break;
  2236. }
  2237. return ifa;
  2238. }
  2239. static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
  2240. {
  2241. struct if6_iter_state *state = seq->private;
  2242. ifa = ifa->lst_next;
  2243. try_again:
  2244. if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
  2245. ifa = inet6_addr_lst[state->bucket];
  2246. goto try_again;
  2247. }
  2248. return ifa;
  2249. }
  2250. static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
  2251. {
  2252. struct inet6_ifaddr *ifa = if6_get_first(seq);
  2253. if (ifa)
  2254. while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
  2255. --pos;
  2256. return pos ? NULL : ifa;
  2257. }
  2258. static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
  2259. {
  2260. read_lock_bh(&addrconf_hash_lock);
  2261. return if6_get_idx(seq, *pos);
  2262. }
  2263. static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2264. {
  2265. struct inet6_ifaddr *ifa;
  2266. ifa = if6_get_next(seq, v);
  2267. ++*pos;
  2268. return ifa;
  2269. }
  2270. static void if6_seq_stop(struct seq_file *seq, void *v)
  2271. {
  2272. read_unlock_bh(&addrconf_hash_lock);
  2273. }
  2274. static int if6_seq_show(struct seq_file *seq, void *v)
  2275. {
  2276. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
  2277. seq_printf(seq,
  2278. NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
  2279. NIP6(ifp->addr),
  2280. ifp->idev->dev->ifindex,
  2281. ifp->prefix_len,
  2282. ifp->scope,
  2283. ifp->flags,
  2284. ifp->idev->dev->name);
  2285. return 0;
  2286. }
  2287. static struct seq_operations if6_seq_ops = {
  2288. .start = if6_seq_start,
  2289. .next = if6_seq_next,
  2290. .show = if6_seq_show,
  2291. .stop = if6_seq_stop,
  2292. };
  2293. static int if6_seq_open(struct inode *inode, struct file *file)
  2294. {
  2295. struct seq_file *seq;
  2296. int rc = -ENOMEM;
  2297. struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  2298. if (!s)
  2299. goto out;
  2300. rc = seq_open(file, &if6_seq_ops);
  2301. if (rc)
  2302. goto out_kfree;
  2303. seq = file->private_data;
  2304. seq->private = s;
  2305. out:
  2306. return rc;
  2307. out_kfree:
  2308. kfree(s);
  2309. goto out;
  2310. }
  2311. static const struct file_operations if6_fops = {
  2312. .owner = THIS_MODULE,
  2313. .open = if6_seq_open,
  2314. .read = seq_read,
  2315. .llseek = seq_lseek,
  2316. .release = seq_release_private,
  2317. };
  2318. int __init if6_proc_init(void)
  2319. {
  2320. if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
  2321. return -ENOMEM;
  2322. return 0;
  2323. }
  2324. void if6_proc_exit(void)
  2325. {
  2326. proc_net_remove("if_inet6");
  2327. }
  2328. #endif /* CONFIG_PROC_FS */
  2329. #ifdef CONFIG_IPV6_MIP6
  2330. /* Check if address is a home address configured on any interface. */
  2331. int ipv6_chk_home_addr(struct in6_addr *addr)
  2332. {
  2333. int ret = 0;
  2334. struct inet6_ifaddr * ifp;
  2335. u8 hash = ipv6_addr_hash(addr);
  2336. read_lock_bh(&addrconf_hash_lock);
  2337. for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
  2338. if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
  2339. (ifp->flags & IFA_F_HOMEADDRESS)) {
  2340. ret = 1;
  2341. break;
  2342. }
  2343. }
  2344. read_unlock_bh(&addrconf_hash_lock);
  2345. return ret;
  2346. }
  2347. #endif
  2348. /*
  2349. * Periodic address status verification
  2350. */
  2351. static void addrconf_verify(unsigned long foo)
  2352. {
  2353. struct inet6_ifaddr *ifp;
  2354. unsigned long now, next;
  2355. int i;
  2356. spin_lock_bh(&addrconf_verify_lock);
  2357. now = jiffies;
  2358. next = now + ADDR_CHECK_FREQUENCY;
  2359. del_timer(&addr_chk_timer);
  2360. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  2361. restart:
  2362. read_lock(&addrconf_hash_lock);
  2363. for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
  2364. unsigned long age;
  2365. #ifdef CONFIG_IPV6_PRIVACY
  2366. unsigned long regen_advance;
  2367. #endif
  2368. if (ifp->flags & IFA_F_PERMANENT)
  2369. continue;
  2370. spin_lock(&ifp->lock);
  2371. age = (now - ifp->tstamp) / HZ;
  2372. #ifdef CONFIG_IPV6_PRIVACY
  2373. regen_advance = ifp->idev->cnf.regen_max_retry *
  2374. ifp->idev->cnf.dad_transmits *
  2375. ifp->idev->nd_parms->retrans_time / HZ;
  2376. #endif
  2377. if (ifp->valid_lft != INFINITY_LIFE_TIME &&
  2378. age >= ifp->valid_lft) {
  2379. spin_unlock(&ifp->lock);
  2380. in6_ifa_hold(ifp);
  2381. read_unlock(&addrconf_hash_lock);
  2382. ipv6_del_addr(ifp);
  2383. goto restart;
  2384. } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
  2385. spin_unlock(&ifp->lock);
  2386. continue;
  2387. } else if (age >= ifp->prefered_lft) {
  2388. /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
  2389. int deprecate = 0;
  2390. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  2391. deprecate = 1;
  2392. ifp->flags |= IFA_F_DEPRECATED;
  2393. }
  2394. if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
  2395. next = ifp->tstamp + ifp->valid_lft * HZ;
  2396. spin_unlock(&ifp->lock);
  2397. if (deprecate) {
  2398. in6_ifa_hold(ifp);
  2399. read_unlock(&addrconf_hash_lock);
  2400. ipv6_ifa_notify(0, ifp);
  2401. in6_ifa_put(ifp);
  2402. goto restart;
  2403. }
  2404. #ifdef CONFIG_IPV6_PRIVACY
  2405. } else if ((ifp->flags&IFA_F_TEMPORARY) &&
  2406. !(ifp->flags&IFA_F_TENTATIVE)) {
  2407. if (age >= ifp->prefered_lft - regen_advance) {
  2408. struct inet6_ifaddr *ifpub = ifp->ifpub;
  2409. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  2410. next = ifp->tstamp + ifp->prefered_lft * HZ;
  2411. if (!ifp->regen_count && ifpub) {
  2412. ifp->regen_count++;
  2413. in6_ifa_hold(ifp);
  2414. in6_ifa_hold(ifpub);
  2415. spin_unlock(&ifp->lock);
  2416. read_unlock(&addrconf_hash_lock);
  2417. spin_lock(&ifpub->lock);
  2418. ifpub->regen_count = 0;
  2419. spin_unlock(&ifpub->lock);
  2420. ipv6_create_tempaddr(ifpub, ifp);
  2421. in6_ifa_put(ifpub);
  2422. in6_ifa_put(ifp);
  2423. goto restart;
  2424. }
  2425. } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
  2426. next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
  2427. spin_unlock(&ifp->lock);
  2428. #endif
  2429. } else {
  2430. /* ifp->prefered_lft <= ifp->valid_lft */
  2431. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  2432. next = ifp->tstamp + ifp->prefered_lft * HZ;
  2433. spin_unlock(&ifp->lock);
  2434. }
  2435. }
  2436. read_unlock(&addrconf_hash_lock);
  2437. }
  2438. addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
  2439. add_timer(&addr_chk_timer);
  2440. spin_unlock_bh(&addrconf_verify_lock);
  2441. }
  2442. static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
  2443. {
  2444. struct in6_addr *pfx = NULL;
  2445. if (addr)
  2446. pfx = nla_data(addr);
  2447. if (local) {
  2448. if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
  2449. pfx = NULL;
  2450. else
  2451. pfx = nla_data(local);
  2452. }
  2453. return pfx;
  2454. }
  2455. static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
  2456. [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
  2457. [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
  2458. [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
  2459. };
  2460. static int
  2461. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  2462. {
  2463. struct ifaddrmsg *ifm;
  2464. struct nlattr *tb[IFA_MAX+1];
  2465. struct in6_addr *pfx;
  2466. int err;
  2467. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
  2468. if (err < 0)
  2469. return err;
  2470. ifm = nlmsg_data(nlh);
  2471. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
  2472. if (pfx == NULL)
  2473. return -EINVAL;
  2474. return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
  2475. }
  2476. static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
  2477. u32 prefered_lft, u32 valid_lft)
  2478. {
  2479. if (!valid_lft || (prefered_lft > valid_lft))
  2480. return -EINVAL;
  2481. if (valid_lft == INFINITY_LIFE_TIME)
  2482. ifa_flags |= IFA_F_PERMANENT;
  2483. else if (valid_lft >= 0x7FFFFFFF/HZ)
  2484. valid_lft = 0x7FFFFFFF/HZ;
  2485. if (prefered_lft == 0)
  2486. ifa_flags |= IFA_F_DEPRECATED;
  2487. else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
  2488. (prefered_lft != INFINITY_LIFE_TIME))
  2489. prefered_lft = 0x7FFFFFFF/HZ;
  2490. spin_lock_bh(&ifp->lock);
  2491. ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
  2492. ifp->tstamp = jiffies;
  2493. ifp->valid_lft = valid_lft;
  2494. ifp->prefered_lft = prefered_lft;
  2495. spin_unlock_bh(&ifp->lock);
  2496. if (!(ifp->flags&IFA_F_TENTATIVE))
  2497. ipv6_ifa_notify(0, ifp);
  2498. addrconf_verify(0);
  2499. return 0;
  2500. }
  2501. static int
  2502. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  2503. {
  2504. struct ifaddrmsg *ifm;
  2505. struct nlattr *tb[IFA_MAX+1];
  2506. struct in6_addr *pfx;
  2507. struct inet6_ifaddr *ifa;
  2508. struct net_device *dev;
  2509. u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
  2510. u8 ifa_flags;
  2511. int err;
  2512. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
  2513. if (err < 0)
  2514. return err;
  2515. ifm = nlmsg_data(nlh);
  2516. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
  2517. if (pfx == NULL)
  2518. return -EINVAL;
  2519. if (tb[IFA_CACHEINFO]) {
  2520. struct ifa_cacheinfo *ci;
  2521. ci = nla_data(tb[IFA_CACHEINFO]);
  2522. valid_lft = ci->ifa_valid;
  2523. preferred_lft = ci->ifa_prefered;
  2524. } else {
  2525. preferred_lft = INFINITY_LIFE_TIME;
  2526. valid_lft = INFINITY_LIFE_TIME;
  2527. }
  2528. dev = __dev_get_by_index(ifm->ifa_index);
  2529. if (dev == NULL)
  2530. return -ENODEV;
  2531. /* We ignore other flags so far. */
  2532. ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
  2533. ifa = ipv6_get_ifaddr(pfx, dev, 1);
  2534. if (ifa == NULL) {
  2535. /*
  2536. * It would be best to check for !NLM_F_CREATE here but
  2537. * userspace alreay relies on not having to provide this.
  2538. */
  2539. return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
  2540. ifa_flags, preferred_lft, valid_lft);
  2541. }
  2542. if (nlh->nlmsg_flags & NLM_F_EXCL ||
  2543. !(nlh->nlmsg_flags & NLM_F_REPLACE))
  2544. err = -EEXIST;
  2545. else
  2546. err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
  2547. in6_ifa_put(ifa);
  2548. return err;
  2549. }
  2550. static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
  2551. u8 scope, int ifindex)
  2552. {
  2553. struct ifaddrmsg *ifm;
  2554. ifm = nlmsg_data(nlh);
  2555. ifm->ifa_family = AF_INET6;
  2556. ifm->ifa_prefixlen = prefixlen;
  2557. ifm->ifa_flags = flags;
  2558. ifm->ifa_scope = scope;
  2559. ifm->ifa_index = ifindex;
  2560. }
  2561. static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
  2562. unsigned long tstamp, u32 preferred, u32 valid)
  2563. {
  2564. struct ifa_cacheinfo ci;
  2565. ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
  2566. + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
  2567. ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
  2568. + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
  2569. ci.ifa_prefered = preferred;
  2570. ci.ifa_valid = valid;
  2571. return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  2572. }
  2573. static inline int rt_scope(int ifa_scope)
  2574. {
  2575. if (ifa_scope & IFA_HOST)
  2576. return RT_SCOPE_HOST;
  2577. else if (ifa_scope & IFA_LINK)
  2578. return RT_SCOPE_LINK;
  2579. else if (ifa_scope & IFA_SITE)
  2580. return RT_SCOPE_SITE;
  2581. else
  2582. return RT_SCOPE_UNIVERSE;
  2583. }
  2584. static inline int inet6_ifaddr_msgsize(void)
  2585. {
  2586. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  2587. + nla_total_size(16) /* IFA_ADDRESS */
  2588. + nla_total_size(sizeof(struct ifa_cacheinfo));
  2589. }
  2590. static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
  2591. u32 pid, u32 seq, int event, unsigned int flags)
  2592. {
  2593. struct nlmsghdr *nlh;
  2594. u32 preferred, valid;
  2595. nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
  2596. if (nlh == NULL)
  2597. return -EMSGSIZE;
  2598. put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
  2599. ifa->idev->dev->ifindex);
  2600. if (!(ifa->flags&IFA_F_PERMANENT)) {
  2601. preferred = ifa->prefered_lft;
  2602. valid = ifa->valid_lft;
  2603. if (preferred != INFINITY_LIFE_TIME) {
  2604. long tval = (jiffies - ifa->tstamp)/HZ;
  2605. preferred -= tval;
  2606. if (valid != INFINITY_LIFE_TIME)
  2607. valid -= tval;
  2608. }
  2609. } else {
  2610. preferred = INFINITY_LIFE_TIME;
  2611. valid = INFINITY_LIFE_TIME;
  2612. }
  2613. if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
  2614. put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
  2615. nlmsg_cancel(skb, nlh);
  2616. return -EMSGSIZE;
  2617. }
  2618. return nlmsg_end(skb, nlh);
  2619. }
  2620. static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
  2621. u32 pid, u32 seq, int event, u16 flags)
  2622. {
  2623. struct nlmsghdr *nlh;
  2624. u8 scope = RT_SCOPE_UNIVERSE;
  2625. int ifindex = ifmca->idev->dev->ifindex;
  2626. if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
  2627. scope = RT_SCOPE_SITE;
  2628. nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
  2629. if (nlh == NULL)
  2630. return -EMSGSIZE;
  2631. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  2632. if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
  2633. put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
  2634. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  2635. nlmsg_cancel(skb, nlh);
  2636. return -EMSGSIZE;
  2637. }
  2638. return nlmsg_end(skb, nlh);
  2639. }
  2640. static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
  2641. u32 pid, u32 seq, int event, unsigned int flags)
  2642. {
  2643. struct nlmsghdr *nlh;
  2644. u8 scope = RT_SCOPE_UNIVERSE;
  2645. int ifindex = ifaca->aca_idev->dev->ifindex;
  2646. if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
  2647. scope = RT_SCOPE_SITE;
  2648. nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
  2649. if (nlh == NULL)
  2650. return -EMSGSIZE;
  2651. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  2652. if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
  2653. put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
  2654. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  2655. nlmsg_cancel(skb, nlh);
  2656. return -EMSGSIZE;
  2657. }
  2658. return nlmsg_end(skb, nlh);
  2659. }
  2660. enum addr_type_t
  2661. {
  2662. UNICAST_ADDR,
  2663. MULTICAST_ADDR,
  2664. ANYCAST_ADDR,
  2665. };
  2666. static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
  2667. enum addr_type_t type)
  2668. {
  2669. int idx, ip_idx;
  2670. int s_idx, s_ip_idx;
  2671. int err = 1;
  2672. struct net_device *dev;
  2673. struct inet6_dev *idev = NULL;
  2674. struct inet6_ifaddr *ifa;
  2675. struct ifmcaddr6 *ifmca;
  2676. struct ifacaddr6 *ifaca;
  2677. s_idx = cb->args[0];
  2678. s_ip_idx = ip_idx = cb->args[1];
  2679. read_lock(&dev_base_lock);
  2680. for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
  2681. if (idx < s_idx)
  2682. continue;
  2683. if (idx > s_idx)
  2684. s_ip_idx = 0;
  2685. ip_idx = 0;
  2686. if ((idev = in6_dev_get(dev)) == NULL)
  2687. continue;
  2688. read_lock_bh(&idev->lock);
  2689. switch (type) {
  2690. case UNICAST_ADDR:
  2691. /* unicast address incl. temp addr */
  2692. for (ifa = idev->addr_list; ifa;
  2693. ifa = ifa->if_next, ip_idx++) {
  2694. if (ip_idx < s_ip_idx)
  2695. continue;
  2696. if ((err = inet6_fill_ifaddr(skb, ifa,
  2697. NETLINK_CB(cb->skb).pid,
  2698. cb->nlh->nlmsg_seq, RTM_NEWADDR,
  2699. NLM_F_MULTI)) <= 0)
  2700. goto done;
  2701. }
  2702. break;
  2703. case MULTICAST_ADDR:
  2704. /* multicast address */
  2705. for (ifmca = idev->mc_list; ifmca;
  2706. ifmca = ifmca->next, ip_idx++) {
  2707. if (ip_idx < s_ip_idx)
  2708. continue;
  2709. if ((err = inet6_fill_ifmcaddr(skb, ifmca,
  2710. NETLINK_CB(cb->skb).pid,
  2711. cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
  2712. NLM_F_MULTI)) <= 0)
  2713. goto done;
  2714. }
  2715. break;
  2716. case ANYCAST_ADDR:
  2717. /* anycast address */
  2718. for (ifaca = idev->ac_list; ifaca;
  2719. ifaca = ifaca->aca_next, ip_idx++) {
  2720. if (ip_idx < s_ip_idx)
  2721. continue;
  2722. if ((err = inet6_fill_ifacaddr(skb, ifaca,
  2723. NETLINK_CB(cb->skb).pid,
  2724. cb->nlh->nlmsg_seq, RTM_GETANYCAST,
  2725. NLM_F_MULTI)) <= 0)
  2726. goto done;
  2727. }
  2728. break;
  2729. default:
  2730. break;
  2731. }
  2732. read_unlock_bh(&idev->lock);
  2733. in6_dev_put(idev);
  2734. }
  2735. done:
  2736. if (err <= 0) {
  2737. read_unlock_bh(&idev->lock);
  2738. in6_dev_put(idev);
  2739. }
  2740. read_unlock(&dev_base_lock);
  2741. cb->args[0] = idx;
  2742. cb->args[1] = ip_idx;
  2743. return skb->len;
  2744. }
  2745. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  2746. {
  2747. enum addr_type_t type = UNICAST_ADDR;
  2748. return inet6_dump_addr(skb, cb, type);
  2749. }
  2750. static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
  2751. {
  2752. enum addr_type_t type = MULTICAST_ADDR;
  2753. return inet6_dump_addr(skb, cb, type);
  2754. }
  2755. static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
  2756. {
  2757. enum addr_type_t type = ANYCAST_ADDR;
  2758. return inet6_dump_addr(skb, cb, type);
  2759. }
  2760. static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
  2761. void *arg)
  2762. {
  2763. struct ifaddrmsg *ifm;
  2764. struct nlattr *tb[IFA_MAX+1];
  2765. struct in6_addr *addr = NULL;
  2766. struct net_device *dev = NULL;
  2767. struct inet6_ifaddr *ifa;
  2768. struct sk_buff *skb;
  2769. int err;
  2770. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
  2771. if (err < 0)
  2772. goto errout;
  2773. addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
  2774. if (addr == NULL) {
  2775. err = -EINVAL;
  2776. goto errout;
  2777. }
  2778. ifm = nlmsg_data(nlh);
  2779. if (ifm->ifa_index)
  2780. dev = __dev_get_by_index(ifm->ifa_index);
  2781. if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
  2782. err = -EADDRNOTAVAIL;
  2783. goto errout;
  2784. }
  2785. if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
  2786. err = -ENOBUFS;
  2787. goto errout_ifa;
  2788. }
  2789. err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
  2790. nlh->nlmsg_seq, RTM_NEWADDR, 0);
  2791. if (err < 0) {
  2792. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  2793. WARN_ON(err == -EMSGSIZE);
  2794. kfree_skb(skb);
  2795. goto errout_ifa;
  2796. }
  2797. err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
  2798. errout_ifa:
  2799. in6_ifa_put(ifa);
  2800. errout:
  2801. return err;
  2802. }
  2803. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  2804. {
  2805. struct sk_buff *skb;
  2806. int err = -ENOBUFS;
  2807. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
  2808. if (skb == NULL)
  2809. goto errout;
  2810. err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
  2811. if (err < 0) {
  2812. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  2813. WARN_ON(err == -EMSGSIZE);
  2814. kfree_skb(skb);
  2815. goto errout;
  2816. }
  2817. err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
  2818. errout:
  2819. if (err < 0)
  2820. rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
  2821. }
  2822. static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
  2823. __s32 *array, int bytes)
  2824. {
  2825. BUG_ON(bytes < (DEVCONF_MAX * 4));
  2826. memset(array, 0, bytes);
  2827. array[DEVCONF_FORWARDING] = cnf->forwarding;
  2828. array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
  2829. array[DEVCONF_MTU6] = cnf->mtu6;
  2830. array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
  2831. array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
  2832. array[DEVCONF_AUTOCONF] = cnf->autoconf;
  2833. array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
  2834. array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
  2835. array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
  2836. array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
  2837. array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
  2838. #ifdef CONFIG_IPV6_PRIVACY
  2839. array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
  2840. array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
  2841. array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
  2842. array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
  2843. array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
  2844. #endif
  2845. array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
  2846. array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
  2847. array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
  2848. #ifdef CONFIG_IPV6_ROUTER_PREF
  2849. array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
  2850. array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
  2851. #ifdef CONFIG_IPV6_ROUTE_INFO
  2852. array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
  2853. #endif
  2854. #endif
  2855. array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
  2856. }
  2857. static inline size_t inet6_if_nlmsg_size(void)
  2858. {
  2859. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  2860. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  2861. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  2862. + nla_total_size(4) /* IFLA_MTU */
  2863. + nla_total_size(4) /* IFLA_LINK */
  2864. + nla_total_size( /* IFLA_PROTINFO */
  2865. nla_total_size(4) /* IFLA_INET6_FLAGS */
  2866. + nla_total_size(sizeof(struct ifla_cacheinfo))
  2867. + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
  2868. );
  2869. }
  2870. static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
  2871. u32 pid, u32 seq, int event, unsigned int flags)
  2872. {
  2873. struct net_device *dev = idev->dev;
  2874. struct nlattr *conf;
  2875. struct ifinfomsg *hdr;
  2876. struct nlmsghdr *nlh;
  2877. void *protoinfo;
  2878. struct ifla_cacheinfo ci;
  2879. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  2880. if (nlh == NULL)
  2881. return -EMSGSIZE;
  2882. hdr = nlmsg_data(nlh);
  2883. hdr->ifi_family = AF_INET6;
  2884. hdr->__ifi_pad = 0;
  2885. hdr->ifi_type = dev->type;
  2886. hdr->ifi_index = dev->ifindex;
  2887. hdr->ifi_flags = dev_get_flags(dev);
  2888. hdr->ifi_change = 0;
  2889. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  2890. if (dev->addr_len)
  2891. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  2892. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  2893. if (dev->ifindex != dev->iflink)
  2894. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  2895. protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
  2896. if (protoinfo == NULL)
  2897. goto nla_put_failure;
  2898. NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
  2899. ci.max_reasm_len = IPV6_MAXPLEN;
  2900. ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
  2901. + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
  2902. ci.reachable_time = idev->nd_parms->reachable_time;
  2903. ci.retrans_time = idev->nd_parms->retrans_time;
  2904. NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
  2905. conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
  2906. if (conf == NULL)
  2907. goto nla_put_failure;
  2908. ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
  2909. /* XXX - Statistics/MC not implemented */
  2910. nla_nest_end(skb, protoinfo);
  2911. return nlmsg_end(skb, nlh);
  2912. nla_put_failure:
  2913. nlmsg_cancel(skb, nlh);
  2914. return -EMSGSIZE;
  2915. }
  2916. static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  2917. {
  2918. int idx, err;
  2919. int s_idx = cb->args[0];
  2920. struct net_device *dev;
  2921. struct inet6_dev *idev;
  2922. read_lock(&dev_base_lock);
  2923. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  2924. if (idx < s_idx)
  2925. continue;
  2926. if ((idev = in6_dev_get(dev)) == NULL)
  2927. continue;
  2928. err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
  2929. cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
  2930. in6_dev_put(idev);
  2931. if (err <= 0)
  2932. break;
  2933. }
  2934. read_unlock(&dev_base_lock);
  2935. cb->args[0] = idx;
  2936. return skb->len;
  2937. }
  2938. void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
  2939. {
  2940. struct sk_buff *skb;
  2941. int err = -ENOBUFS;
  2942. skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
  2943. if (skb == NULL)
  2944. goto errout;
  2945. err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
  2946. if (err < 0) {
  2947. /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
  2948. WARN_ON(err == -EMSGSIZE);
  2949. kfree_skb(skb);
  2950. goto errout;
  2951. }
  2952. err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
  2953. errout:
  2954. if (err < 0)
  2955. rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
  2956. }
  2957. static inline size_t inet6_prefix_nlmsg_size(void)
  2958. {
  2959. return NLMSG_ALIGN(sizeof(struct prefixmsg))
  2960. + nla_total_size(sizeof(struct in6_addr))
  2961. + nla_total_size(sizeof(struct prefix_cacheinfo));
  2962. }
  2963. static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
  2964. struct prefix_info *pinfo, u32 pid, u32 seq,
  2965. int event, unsigned int flags)
  2966. {
  2967. struct prefixmsg *pmsg;
  2968. struct nlmsghdr *nlh;
  2969. struct prefix_cacheinfo ci;
  2970. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
  2971. if (nlh == NULL)
  2972. return -EMSGSIZE;
  2973. pmsg = nlmsg_data(nlh);
  2974. pmsg->prefix_family = AF_INET6;
  2975. pmsg->prefix_pad1 = 0;
  2976. pmsg->prefix_pad2 = 0;
  2977. pmsg->prefix_ifindex = idev->dev->ifindex;
  2978. pmsg->prefix_len = pinfo->prefix_len;
  2979. pmsg->prefix_type = pinfo->type;
  2980. pmsg->prefix_pad3 = 0;
  2981. pmsg->prefix_flags = 0;
  2982. if (pinfo->onlink)
  2983. pmsg->prefix_flags |= IF_PREFIX_ONLINK;
  2984. if (pinfo->autoconf)
  2985. pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
  2986. NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
  2987. ci.preferred_time = ntohl(pinfo->prefered);
  2988. ci.valid_time = ntohl(pinfo->valid);
  2989. NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
  2990. return nlmsg_end(skb, nlh);
  2991. nla_put_failure:
  2992. nlmsg_cancel(skb, nlh);
  2993. return -EMSGSIZE;
  2994. }
  2995. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  2996. struct prefix_info *pinfo)
  2997. {
  2998. struct sk_buff *skb;
  2999. int err = -ENOBUFS;
  3000. skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
  3001. if (skb == NULL)
  3002. goto errout;
  3003. err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
  3004. if (err < 0) {
  3005. /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
  3006. WARN_ON(err == -EMSGSIZE);
  3007. kfree_skb(skb);
  3008. goto errout;
  3009. }
  3010. err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
  3011. errout:
  3012. if (err < 0)
  3013. rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
  3014. }
  3015. static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
  3016. [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
  3017. [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
  3018. [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
  3019. [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
  3020. .dumpit = inet6_dump_ifaddr, },
  3021. [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
  3022. [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
  3023. [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
  3024. [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
  3025. [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
  3026. .dumpit = inet6_dump_fib, },
  3027. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  3028. [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
  3029. #endif
  3030. };
  3031. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  3032. {
  3033. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  3034. switch (event) {
  3035. case RTM_NEWADDR:
  3036. ip6_ins_rt(ifp->rt);
  3037. if (ifp->idev->cnf.forwarding)
  3038. addrconf_join_anycast(ifp);
  3039. break;
  3040. case RTM_DELADDR:
  3041. if (ifp->idev->cnf.forwarding)
  3042. addrconf_leave_anycast(ifp);
  3043. addrconf_leave_solict(ifp->idev, &ifp->addr);
  3044. dst_hold(&ifp->rt->u.dst);
  3045. if (ip6_del_rt(ifp->rt))
  3046. dst_free(&ifp->rt->u.dst);
  3047. break;
  3048. }
  3049. }
  3050. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  3051. {
  3052. rcu_read_lock_bh();
  3053. if (likely(ifp->idev->dead == 0))
  3054. __ipv6_ifa_notify(event, ifp);
  3055. rcu_read_unlock_bh();
  3056. }
  3057. #ifdef CONFIG_SYSCTL
  3058. static
  3059. int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
  3060. void __user *buffer, size_t *lenp, loff_t *ppos)
  3061. {
  3062. int *valp = ctl->data;
  3063. int val = *valp;
  3064. int ret;
  3065. ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  3066. if (write && valp != &ipv6_devconf_dflt.forwarding) {
  3067. if (valp != &ipv6_devconf.forwarding) {
  3068. if ((!*valp) ^ (!val)) {
  3069. struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
  3070. if (idev == NULL)
  3071. return ret;
  3072. dev_forward_change(idev);
  3073. }
  3074. } else {
  3075. ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
  3076. addrconf_forward_change();
  3077. }
  3078. if (*valp)
  3079. rt6_purge_dflt_routers();
  3080. }
  3081. return ret;
  3082. }
  3083. static int addrconf_sysctl_forward_strategy(ctl_table *table,
  3084. int __user *name, int nlen,
  3085. void __user *oldval,
  3086. size_t __user *oldlenp,
  3087. void __user *newval, size_t newlen)
  3088. {
  3089. int *valp = table->data;
  3090. int new;
  3091. if (!newval || !newlen)
  3092. return 0;
  3093. if (newlen != sizeof(int))
  3094. return -EINVAL;
  3095. if (get_user(new, (int __user *)newval))
  3096. return -EFAULT;
  3097. if (new == *valp)
  3098. return 0;
  3099. if (oldval && oldlenp) {
  3100. size_t len;
  3101. if (get_user(len, oldlenp))
  3102. return -EFAULT;
  3103. if (len) {
  3104. if (len > table->maxlen)
  3105. len = table->maxlen;
  3106. if (copy_to_user(oldval, valp, len))
  3107. return -EFAULT;
  3108. if (put_user(len, oldlenp))
  3109. return -EFAULT;
  3110. }
  3111. }
  3112. if (valp != &ipv6_devconf_dflt.forwarding) {
  3113. if (valp != &ipv6_devconf.forwarding) {
  3114. struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
  3115. int changed;
  3116. if (unlikely(idev == NULL))
  3117. return -ENODEV;
  3118. changed = (!*valp) ^ (!new);
  3119. *valp = new;
  3120. if (changed)
  3121. dev_forward_change(idev);
  3122. } else {
  3123. *valp = new;
  3124. addrconf_forward_change();
  3125. }
  3126. if (*valp)
  3127. rt6_purge_dflt_routers();
  3128. } else
  3129. *valp = new;
  3130. return 1;
  3131. }
  3132. static struct addrconf_sysctl_table
  3133. {
  3134. struct ctl_table_header *sysctl_header;
  3135. ctl_table addrconf_vars[__NET_IPV6_MAX];
  3136. ctl_table addrconf_dev[2];
  3137. ctl_table addrconf_conf_dir[2];
  3138. ctl_table addrconf_proto_dir[2];
  3139. ctl_table addrconf_root_dir[2];
  3140. } addrconf_sysctl __read_mostly = {
  3141. .sysctl_header = NULL,
  3142. .addrconf_vars = {
  3143. {
  3144. .ctl_name = NET_IPV6_FORWARDING,
  3145. .procname = "forwarding",
  3146. .data = &ipv6_devconf.forwarding,
  3147. .maxlen = sizeof(int),
  3148. .mode = 0644,
  3149. .proc_handler = &addrconf_sysctl_forward,
  3150. .strategy = &addrconf_sysctl_forward_strategy,
  3151. },
  3152. {
  3153. .ctl_name = NET_IPV6_HOP_LIMIT,
  3154. .procname = "hop_limit",
  3155. .data = &ipv6_devconf.hop_limit,
  3156. .maxlen = sizeof(int),
  3157. .mode = 0644,
  3158. .proc_handler = proc_dointvec,
  3159. },
  3160. {
  3161. .ctl_name = NET_IPV6_MTU,
  3162. .procname = "mtu",
  3163. .data = &ipv6_devconf.mtu6,
  3164. .maxlen = sizeof(int),
  3165. .mode = 0644,
  3166. .proc_handler = &proc_dointvec,
  3167. },
  3168. {
  3169. .ctl_name = NET_IPV6_ACCEPT_RA,
  3170. .procname = "accept_ra",
  3171. .data = &ipv6_devconf.accept_ra,
  3172. .maxlen = sizeof(int),
  3173. .mode = 0644,
  3174. .proc_handler = &proc_dointvec,
  3175. },
  3176. {
  3177. .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
  3178. .procname = "accept_redirects",
  3179. .data = &ipv6_devconf.accept_redirects,
  3180. .maxlen = sizeof(int),
  3181. .mode = 0644,
  3182. .proc_handler = &proc_dointvec,
  3183. },
  3184. {
  3185. .ctl_name = NET_IPV6_AUTOCONF,
  3186. .procname = "autoconf",
  3187. .data = &ipv6_devconf.autoconf,
  3188. .maxlen = sizeof(int),
  3189. .mode = 0644,
  3190. .proc_handler = &proc_dointvec,
  3191. },
  3192. {
  3193. .ctl_name = NET_IPV6_DAD_TRANSMITS,
  3194. .procname = "dad_transmits",
  3195. .data = &ipv6_devconf.dad_transmits,
  3196. .maxlen = sizeof(int),
  3197. .mode = 0644,
  3198. .proc_handler = &proc_dointvec,
  3199. },
  3200. {
  3201. .ctl_name = NET_IPV6_RTR_SOLICITS,
  3202. .procname = "router_solicitations",
  3203. .data = &ipv6_devconf.rtr_solicits,
  3204. .maxlen = sizeof(int),
  3205. .mode = 0644,
  3206. .proc_handler = &proc_dointvec,
  3207. },
  3208. {
  3209. .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
  3210. .procname = "router_solicitation_interval",
  3211. .data = &ipv6_devconf.rtr_solicit_interval,
  3212. .maxlen = sizeof(int),
  3213. .mode = 0644,
  3214. .proc_handler = &proc_dointvec_jiffies,
  3215. .strategy = &sysctl_jiffies,
  3216. },
  3217. {
  3218. .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
  3219. .procname = "router_solicitation_delay",
  3220. .data = &ipv6_devconf.rtr_solicit_delay,
  3221. .maxlen = sizeof(int),
  3222. .mode = 0644,
  3223. .proc_handler = &proc_dointvec_jiffies,
  3224. .strategy = &sysctl_jiffies,
  3225. },
  3226. {
  3227. .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
  3228. .procname = "force_mld_version",
  3229. .data = &ipv6_devconf.force_mld_version,
  3230. .maxlen = sizeof(int),
  3231. .mode = 0644,
  3232. .proc_handler = &proc_dointvec,
  3233. },
  3234. #ifdef CONFIG_IPV6_PRIVACY
  3235. {
  3236. .ctl_name = NET_IPV6_USE_TEMPADDR,
  3237. .procname = "use_tempaddr",
  3238. .data = &ipv6_devconf.use_tempaddr,
  3239. .maxlen = sizeof(int),
  3240. .mode = 0644,
  3241. .proc_handler = &proc_dointvec,
  3242. },
  3243. {
  3244. .ctl_name = NET_IPV6_TEMP_VALID_LFT,
  3245. .procname = "temp_valid_lft",
  3246. .data = &ipv6_devconf.temp_valid_lft,
  3247. .maxlen = sizeof(int),
  3248. .mode = 0644,
  3249. .proc_handler = &proc_dointvec,
  3250. },
  3251. {
  3252. .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
  3253. .procname = "temp_prefered_lft",
  3254. .data = &ipv6_devconf.temp_prefered_lft,
  3255. .maxlen = sizeof(int),
  3256. .mode = 0644,
  3257. .proc_handler = &proc_dointvec,
  3258. },
  3259. {
  3260. .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
  3261. .procname = "regen_max_retry",
  3262. .data = &ipv6_devconf.regen_max_retry,
  3263. .maxlen = sizeof(int),
  3264. .mode = 0644,
  3265. .proc_handler = &proc_dointvec,
  3266. },
  3267. {
  3268. .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
  3269. .procname = "max_desync_factor",
  3270. .data = &ipv6_devconf.max_desync_factor,
  3271. .maxlen = sizeof(int),
  3272. .mode = 0644,
  3273. .proc_handler = &proc_dointvec,
  3274. },
  3275. #endif
  3276. {
  3277. .ctl_name = NET_IPV6_MAX_ADDRESSES,
  3278. .procname = "max_addresses",
  3279. .data = &ipv6_devconf.max_addresses,
  3280. .maxlen = sizeof(int),
  3281. .mode = 0644,
  3282. .proc_handler = &proc_dointvec,
  3283. },
  3284. {
  3285. .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
  3286. .procname = "accept_ra_defrtr",
  3287. .data = &ipv6_devconf.accept_ra_defrtr,
  3288. .maxlen = sizeof(int),
  3289. .mode = 0644,
  3290. .proc_handler = &proc_dointvec,
  3291. },
  3292. {
  3293. .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
  3294. .procname = "accept_ra_pinfo",
  3295. .data = &ipv6_devconf.accept_ra_pinfo,
  3296. .maxlen = sizeof(int),
  3297. .mode = 0644,
  3298. .proc_handler = &proc_dointvec,
  3299. },
  3300. #ifdef CONFIG_IPV6_ROUTER_PREF
  3301. {
  3302. .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
  3303. .procname = "accept_ra_rtr_pref",
  3304. .data = &ipv6_devconf.accept_ra_rtr_pref,
  3305. .maxlen = sizeof(int),
  3306. .mode = 0644,
  3307. .proc_handler = &proc_dointvec,
  3308. },
  3309. {
  3310. .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
  3311. .procname = "router_probe_interval",
  3312. .data = &ipv6_devconf.rtr_probe_interval,
  3313. .maxlen = sizeof(int),
  3314. .mode = 0644,
  3315. .proc_handler = &proc_dointvec_jiffies,
  3316. .strategy = &sysctl_jiffies,
  3317. },
  3318. #ifdef CONFIG_IPV6_ROUTE_INFO
  3319. {
  3320. .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
  3321. .procname = "accept_ra_rt_info_max_plen",
  3322. .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
  3323. .maxlen = sizeof(int),
  3324. .mode = 0644,
  3325. .proc_handler = &proc_dointvec,
  3326. },
  3327. #endif
  3328. #endif
  3329. {
  3330. .ctl_name = NET_IPV6_PROXY_NDP,
  3331. .procname = "proxy_ndp",
  3332. .data = &ipv6_devconf.proxy_ndp,
  3333. .maxlen = sizeof(int),
  3334. .mode = 0644,
  3335. .proc_handler = &proc_dointvec,
  3336. },
  3337. {
  3338. .ctl_name = 0, /* sentinel */
  3339. }
  3340. },
  3341. .addrconf_dev = {
  3342. {
  3343. .ctl_name = NET_PROTO_CONF_ALL,
  3344. .procname = "all",
  3345. .mode = 0555,
  3346. .child = addrconf_sysctl.addrconf_vars,
  3347. },
  3348. {
  3349. .ctl_name = 0, /* sentinel */
  3350. }
  3351. },
  3352. .addrconf_conf_dir = {
  3353. {
  3354. .ctl_name = NET_IPV6_CONF,
  3355. .procname = "conf",
  3356. .mode = 0555,
  3357. .child = addrconf_sysctl.addrconf_dev,
  3358. },
  3359. {
  3360. .ctl_name = 0, /* sentinel */
  3361. }
  3362. },
  3363. .addrconf_proto_dir = {
  3364. {
  3365. .ctl_name = NET_IPV6,
  3366. .procname = "ipv6",
  3367. .mode = 0555,
  3368. .child = addrconf_sysctl.addrconf_conf_dir,
  3369. },
  3370. {
  3371. .ctl_name = 0, /* sentinel */
  3372. }
  3373. },
  3374. .addrconf_root_dir = {
  3375. {
  3376. .ctl_name = CTL_NET,
  3377. .procname = "net",
  3378. .mode = 0555,
  3379. .child = addrconf_sysctl.addrconf_proto_dir,
  3380. },
  3381. {
  3382. .ctl_name = 0, /* sentinel */
  3383. }
  3384. },
  3385. };
  3386. static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
  3387. {
  3388. int i;
  3389. struct net_device *dev = idev ? idev->dev : NULL;
  3390. struct addrconf_sysctl_table *t;
  3391. char *dev_name = NULL;
  3392. t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
  3393. if (t == NULL)
  3394. return;
  3395. for (i=0; t->addrconf_vars[i].data; i++) {
  3396. t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
  3397. t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
  3398. }
  3399. if (dev) {
  3400. dev_name = dev->name;
  3401. t->addrconf_dev[0].ctl_name = dev->ifindex;
  3402. } else {
  3403. dev_name = "default";
  3404. t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
  3405. }
  3406. /*
  3407. * Make a copy of dev_name, because '.procname' is regarded as const
  3408. * by sysctl and we wouldn't want anyone to change it under our feet
  3409. * (see SIOCSIFNAME).
  3410. */
  3411. dev_name = kstrdup(dev_name, GFP_KERNEL);
  3412. if (!dev_name)
  3413. goto free;
  3414. t->addrconf_dev[0].procname = dev_name;
  3415. t->addrconf_dev[0].child = t->addrconf_vars;
  3416. t->addrconf_conf_dir[0].child = t->addrconf_dev;
  3417. t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
  3418. t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
  3419. t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
  3420. if (t->sysctl_header == NULL)
  3421. goto free_procname;
  3422. else
  3423. p->sysctl = t;
  3424. return;
  3425. /* error path */
  3426. free_procname:
  3427. kfree(dev_name);
  3428. free:
  3429. kfree(t);
  3430. return;
  3431. }
  3432. static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
  3433. {
  3434. if (p->sysctl) {
  3435. struct addrconf_sysctl_table *t = p->sysctl;
  3436. p->sysctl = NULL;
  3437. unregister_sysctl_table(t->sysctl_header);
  3438. kfree(t->addrconf_dev[0].procname);
  3439. kfree(t);
  3440. }
  3441. }
  3442. #endif
  3443. /*
  3444. * Device notifier
  3445. */
  3446. int register_inet6addr_notifier(struct notifier_block *nb)
  3447. {
  3448. return atomic_notifier_chain_register(&inet6addr_chain, nb);
  3449. }
  3450. int unregister_inet6addr_notifier(struct notifier_block *nb)
  3451. {
  3452. return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
  3453. }
  3454. /*
  3455. * Init / cleanup code
  3456. */
  3457. int __init addrconf_init(void)
  3458. {
  3459. int err = 0;
  3460. /* The addrconf netdev notifier requires that loopback_dev
  3461. * has it's ipv6 private information allocated and setup
  3462. * before it can bring up and give link-local addresses
  3463. * to other devices which are up.
  3464. *
  3465. * Unfortunately, loopback_dev is not necessarily the first
  3466. * entry in the global dev_base list of net devices. In fact,
  3467. * it is likely to be the very last entry on that list.
  3468. * So this causes the notifier registry below to try and
  3469. * give link-local addresses to all devices besides loopback_dev
  3470. * first, then loopback_dev, which cases all the non-loopback_dev
  3471. * devices to fail to get a link-local address.
  3472. *
  3473. * So, as a temporary fix, allocate the ipv6 structure for
  3474. * loopback_dev first by hand.
  3475. * Longer term, all of the dependencies ipv6 has upon the loopback
  3476. * device and it being up should be removed.
  3477. */
  3478. rtnl_lock();
  3479. if (!ipv6_add_dev(&loopback_dev))
  3480. err = -ENOMEM;
  3481. rtnl_unlock();
  3482. if (err)
  3483. return err;
  3484. ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
  3485. register_netdevice_notifier(&ipv6_dev_notf);
  3486. addrconf_verify(0);
  3487. rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
  3488. #ifdef CONFIG_SYSCTL
  3489. addrconf_sysctl.sysctl_header =
  3490. register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
  3491. addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
  3492. #endif
  3493. return 0;
  3494. }
  3495. void __exit addrconf_cleanup(void)
  3496. {
  3497. struct net_device *dev;
  3498. struct inet6_dev *idev;
  3499. struct inet6_ifaddr *ifa;
  3500. int i;
  3501. unregister_netdevice_notifier(&ipv6_dev_notf);
  3502. rtnetlink_links[PF_INET6] = NULL;
  3503. #ifdef CONFIG_SYSCTL
  3504. addrconf_sysctl_unregister(&ipv6_devconf_dflt);
  3505. addrconf_sysctl_unregister(&ipv6_devconf);
  3506. #endif
  3507. rtnl_lock();
  3508. /*
  3509. * clean dev list.
  3510. */
  3511. for (dev=dev_base; dev; dev=dev->next) {
  3512. if ((idev = __in6_dev_get(dev)) == NULL)
  3513. continue;
  3514. addrconf_ifdown(dev, 1);
  3515. }
  3516. addrconf_ifdown(&loopback_dev, 2);
  3517. /*
  3518. * Check hash table.
  3519. */
  3520. write_lock_bh(&addrconf_hash_lock);
  3521. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  3522. for (ifa=inet6_addr_lst[i]; ifa; ) {
  3523. struct inet6_ifaddr *bifa;
  3524. bifa = ifa;
  3525. ifa = ifa->lst_next;
  3526. printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
  3527. /* Do not free it; something is wrong.
  3528. Now we can investigate it with debugger.
  3529. */
  3530. }
  3531. }
  3532. write_unlock_bh(&addrconf_hash_lock);
  3533. del_timer(&addr_chk_timer);
  3534. rtnl_unlock();
  3535. #ifdef CONFIG_PROC_FS
  3536. proc_net_remove("if_inet6");
  3537. #endif
  3538. }