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