addrconf.c 107 KB

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