addrconf.c 125 KB

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