addrconf.c 124 KB

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