addrconf.c 115 KB

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