addrconf.c 103 KB

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