addrconf.c 107 KB

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