addrconf.c 118 KB

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