addrconf.c 104 KB

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