addrconf.c 101 KB

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