addrconf.c 113 KB

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