addrconf.c 106 KB

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