addrconf.c 121 KB

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