addrconf.c 104 KB

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