addrconf.c 103 KB

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