addrconf.c 114 KB

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