addrconf.c 120 KB

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