addrconf.c 101 KB

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