addrconf.c 113 KB

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