addrconf.c 118 KB

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