addrconf.c 104 KB

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