addrconf.c 106 KB

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