addrconf.c 120 KB

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