addrconf.c 106 KB

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