addrconf.c 131 KB

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