addrconf.c 186 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPv6 Address [auto]configuration
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. */
  10. /*
  11. * Changes:
  12. *
  13. * Janos Farkas : delete timer on ifdown
  14. * <chexum@bankinf.banki.hu>
  15. * Andi Kleen : kill double kfree on module
  16. * unload.
  17. * Maciej W. Rozycki : FDDI support
  18. * sekiya@USAGI : Don't send too many RS
  19. * packets.
  20. * yoshfuji@USAGI : Fixed interval between DAD
  21. * packets.
  22. * YOSHIFUJI Hideaki @USAGI : improved accuracy of
  23. * address validation timer.
  24. * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
  25. * support.
  26. * Yuji SEKIYA @USAGI : Don't assign a same IPv6
  27. * address on a same interface.
  28. * YOSHIFUJI Hideaki @USAGI : ARCnet support
  29. * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
  30. * seq_file.
  31. * YOSHIFUJI Hideaki @USAGI : improved source address
  32. * selection; consider scope,
  33. * status etc.
  34. */
  35. #define pr_fmt(fmt) "IPv6: " fmt
  36. #include <linux/errno.h>
  37. #include <linux/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched/signal.h>
  40. #include <linux/socket.h>
  41. #include <linux/sockios.h>
  42. #include <linux/net.h>
  43. #include <linux/inet.h>
  44. #include <linux/in6.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/if_addr.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/if_arcnet.h>
  49. #include <linux/if_infiniband.h>
  50. #include <linux/route.h>
  51. #include <linux/inetdevice.h>
  52. #include <linux/init.h>
  53. #include <linux/slab.h>
  54. #ifdef CONFIG_SYSCTL
  55. #include <linux/sysctl.h>
  56. #endif
  57. #include <linux/capability.h>
  58. #include <linux/delay.h>
  59. #include <linux/notifier.h>
  60. #include <linux/string.h>
  61. #include <linux/hash.h>
  62. #include <net/ip_tunnels.h>
  63. #include <net/net_namespace.h>
  64. #include <net/sock.h>
  65. #include <net/snmp.h>
  66. #include <net/6lowpan.h>
  67. #include <net/firewire.h>
  68. #include <net/ipv6.h>
  69. #include <net/protocol.h>
  70. #include <net/ndisc.h>
  71. #include <net/ip6_route.h>
  72. #include <net/addrconf.h>
  73. #include <net/tcp.h>
  74. #include <net/ip.h>
  75. #include <net/netlink.h>
  76. #include <net/pkt_sched.h>
  77. #include <net/l3mdev.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 <linux/unaligned.h>
  84. #include <linux/proc_fs.h>
  85. #include <linux/seq_file.h>
  86. #include <linux/export.h>
  87. #include <linux/ioam6.h>
  88. #define IPV6_MAX_STRLEN \
  89. sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
  90. static inline u32 cstamp_delta(unsigned long cstamp)
  91. {
  92. return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
  93. }
  94. static inline s32 rfc3315_s14_backoff_init(s32 irt)
  95. {
  96. /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
  97. u64 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)irt;
  98. do_div(tmp, 1000000);
  99. return (s32)tmp;
  100. }
  101. static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
  102. {
  103. /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
  104. u64 tmp = get_random_u32_inclusive(1900000, 2100000) * (u64)rt;
  105. do_div(tmp, 1000000);
  106. if ((s32)tmp > mrt) {
  107. /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
  108. tmp = get_random_u32_inclusive(900000, 1100000) * (u64)mrt;
  109. do_div(tmp, 1000000);
  110. }
  111. return (s32)tmp;
  112. }
  113. #ifdef CONFIG_SYSCTL
  114. static int addrconf_sysctl_register(struct inet6_dev *idev);
  115. static void addrconf_sysctl_unregister(struct inet6_dev *idev);
  116. #else
  117. static inline int addrconf_sysctl_register(struct inet6_dev *idev)
  118. {
  119. return 0;
  120. }
  121. static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
  122. {
  123. }
  124. #endif
  125. static void ipv6_gen_rnd_iid(struct in6_addr *addr);
  126. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
  127. static int ipv6_count_addresses(const struct inet6_dev *idev);
  128. static int ipv6_generate_stable_address(struct in6_addr *addr,
  129. u8 dad_count,
  130. const struct inet6_dev *idev);
  131. #define IN6_ADDR_HSIZE_SHIFT 8
  132. #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
  133. static void addrconf_verify(struct net *net);
  134. static void addrconf_verify_rtnl(struct net *net);
  135. static struct workqueue_struct *addrconf_wq;
  136. static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
  137. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
  138. static void addrconf_type_change(struct net_device *dev,
  139. unsigned long event);
  140. static int addrconf_ifdown(struct net_device *dev, bool unregister);
  141. static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
  142. int plen,
  143. const struct net_device *dev,
  144. u32 flags, u32 noflags,
  145. bool no_gw);
  146. static void addrconf_dad_start(struct inet6_ifaddr *ifp);
  147. static void addrconf_dad_work(struct work_struct *w);
  148. static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
  149. bool send_na);
  150. static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
  151. static void addrconf_rs_timer(struct timer_list *t);
  152. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  153. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  154. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  155. struct prefix_info *pinfo);
  156. static struct ipv6_devconf ipv6_devconf __read_mostly = {
  157. .forwarding = 0,
  158. .hop_limit = IPV6_DEFAULT_HOPLIMIT,
  159. .mtu6 = IPV6_MIN_MTU,
  160. .accept_ra = 1,
  161. .accept_redirects = 1,
  162. .autoconf = 1,
  163. .force_mld_version = 0,
  164. .mldv1_unsolicited_report_interval = 10 * HZ,
  165. .mldv2_unsolicited_report_interval = HZ,
  166. .dad_transmits = 1,
  167. .rtr_solicits = MAX_RTR_SOLICITATIONS,
  168. .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
  169. .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
  170. .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
  171. .use_tempaddr = 0,
  172. .temp_valid_lft = TEMP_VALID_LIFETIME,
  173. .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
  174. .regen_min_advance = REGEN_MIN_ADVANCE,
  175. .regen_max_retry = REGEN_MAX_RETRY,
  176. .max_desync_factor = MAX_DESYNC_FACTOR,
  177. .max_addresses = IPV6_MAX_ADDRESSES,
  178. .accept_ra_defrtr = 1,
  179. .ra_defrtr_metric = IP6_RT_PRIO_USER,
  180. .accept_ra_from_local = 0,
  181. .accept_ra_min_hop_limit= 1,
  182. .accept_ra_min_lft = 0,
  183. .accept_ra_pinfo = 1,
  184. #ifdef CONFIG_IPV6_ROUTER_PREF
  185. .accept_ra_rtr_pref = 1,
  186. .rtr_probe_interval = 60 * HZ,
  187. #ifdef CONFIG_IPV6_ROUTE_INFO
  188. .accept_ra_rt_info_min_plen = 0,
  189. .accept_ra_rt_info_max_plen = 0,
  190. #endif
  191. #endif
  192. .proxy_ndp = 0,
  193. .accept_source_route = 0, /* we do not accept RH0 by default. */
  194. .disable_ipv6 = 0,
  195. .accept_dad = 0,
  196. .suppress_frag_ndisc = 1,
  197. .accept_ra_mtu = 1,
  198. .stable_secret = {
  199. .initialized = false,
  200. },
  201. .use_oif_addrs_only = 0,
  202. .ignore_routes_with_linkdown = 0,
  203. .keep_addr_on_down = 0,
  204. .seg6_enabled = 0,
  205. #ifdef CONFIG_IPV6_SEG6_HMAC
  206. .seg6_require_hmac = 0,
  207. #endif
  208. .enhanced_dad = 1,
  209. .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
  210. .disable_policy = 0,
  211. .rpl_seg_enabled = 0,
  212. .ioam6_enabled = 0,
  213. .ioam6_id = IOAM6_DEFAULT_IF_ID,
  214. .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
  215. .ndisc_evict_nocarrier = 1,
  216. .ra_honor_pio_life = 0,
  217. .ra_honor_pio_pflag = 0,
  218. };
  219. static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
  220. .forwarding = 0,
  221. .hop_limit = IPV6_DEFAULT_HOPLIMIT,
  222. .mtu6 = IPV6_MIN_MTU,
  223. .accept_ra = 1,
  224. .accept_redirects = 1,
  225. .autoconf = 1,
  226. .force_mld_version = 0,
  227. .mldv1_unsolicited_report_interval = 10 * HZ,
  228. .mldv2_unsolicited_report_interval = HZ,
  229. .dad_transmits = 1,
  230. .rtr_solicits = MAX_RTR_SOLICITATIONS,
  231. .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
  232. .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
  233. .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
  234. .use_tempaddr = 0,
  235. .temp_valid_lft = TEMP_VALID_LIFETIME,
  236. .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
  237. .regen_min_advance = REGEN_MIN_ADVANCE,
  238. .regen_max_retry = REGEN_MAX_RETRY,
  239. .max_desync_factor = MAX_DESYNC_FACTOR,
  240. .max_addresses = IPV6_MAX_ADDRESSES,
  241. .accept_ra_defrtr = 1,
  242. .ra_defrtr_metric = IP6_RT_PRIO_USER,
  243. .accept_ra_from_local = 0,
  244. .accept_ra_min_hop_limit= 1,
  245. .accept_ra_min_lft = 0,
  246. .accept_ra_pinfo = 1,
  247. #ifdef CONFIG_IPV6_ROUTER_PREF
  248. .accept_ra_rtr_pref = 1,
  249. .rtr_probe_interval = 60 * HZ,
  250. #ifdef CONFIG_IPV6_ROUTE_INFO
  251. .accept_ra_rt_info_min_plen = 0,
  252. .accept_ra_rt_info_max_plen = 0,
  253. #endif
  254. #endif
  255. .proxy_ndp = 0,
  256. .accept_source_route = 0, /* we do not accept RH0 by default. */
  257. .disable_ipv6 = 0,
  258. .accept_dad = 1,
  259. .suppress_frag_ndisc = 1,
  260. .accept_ra_mtu = 1,
  261. .stable_secret = {
  262. .initialized = false,
  263. },
  264. .use_oif_addrs_only = 0,
  265. .ignore_routes_with_linkdown = 0,
  266. .keep_addr_on_down = 0,
  267. .seg6_enabled = 0,
  268. #ifdef CONFIG_IPV6_SEG6_HMAC
  269. .seg6_require_hmac = 0,
  270. #endif
  271. .enhanced_dad = 1,
  272. .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
  273. .disable_policy = 0,
  274. .rpl_seg_enabled = 0,
  275. .ioam6_enabled = 0,
  276. .ioam6_id = IOAM6_DEFAULT_IF_ID,
  277. .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
  278. .ndisc_evict_nocarrier = 1,
  279. .ra_honor_pio_life = 0,
  280. .ra_honor_pio_pflag = 0,
  281. };
  282. /* Check if link is ready: is it up and is a valid qdisc available */
  283. static inline bool addrconf_link_ready(const struct net_device *dev)
  284. {
  285. return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
  286. }
  287. static void addrconf_del_rs_timer(struct inet6_dev *idev)
  288. {
  289. if (del_timer(&idev->rs_timer))
  290. __in6_dev_put(idev);
  291. }
  292. static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
  293. {
  294. if (cancel_delayed_work(&ifp->dad_work))
  295. __in6_ifa_put(ifp);
  296. }
  297. static void addrconf_mod_rs_timer(struct inet6_dev *idev,
  298. unsigned long when)
  299. {
  300. if (!mod_timer(&idev->rs_timer, jiffies + when))
  301. in6_dev_hold(idev);
  302. }
  303. static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
  304. unsigned long delay)
  305. {
  306. in6_ifa_hold(ifp);
  307. if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
  308. in6_ifa_put(ifp);
  309. }
  310. static int snmp6_alloc_dev(struct inet6_dev *idev)
  311. {
  312. int i;
  313. idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT);
  314. if (!idev->stats.ipv6)
  315. goto err_ip;
  316. for_each_possible_cpu(i) {
  317. struct ipstats_mib *addrconf_stats;
  318. addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
  319. u64_stats_init(&addrconf_stats->syncp);
  320. }
  321. idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
  322. GFP_KERNEL);
  323. if (!idev->stats.icmpv6dev)
  324. goto err_icmp;
  325. idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
  326. GFP_KERNEL_ACCOUNT);
  327. if (!idev->stats.icmpv6msgdev)
  328. goto err_icmpmsg;
  329. return 0;
  330. err_icmpmsg:
  331. kfree(idev->stats.icmpv6dev);
  332. err_icmp:
  333. free_percpu(idev->stats.ipv6);
  334. err_ip:
  335. return -ENOMEM;
  336. }
  337. static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
  338. {
  339. struct inet6_dev *ndev;
  340. int err = -ENOMEM;
  341. ASSERT_RTNL();
  342. if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev)
  343. return ERR_PTR(-EINVAL);
  344. ndev = kzalloc(sizeof(*ndev), GFP_KERNEL_ACCOUNT);
  345. if (!ndev)
  346. return ERR_PTR(err);
  347. rwlock_init(&ndev->lock);
  348. ndev->dev = dev;
  349. INIT_LIST_HEAD(&ndev->addr_list);
  350. timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
  351. memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
  352. if (ndev->cnf.stable_secret.initialized)
  353. ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
  354. ndev->cnf.mtu6 = dev->mtu;
  355. ndev->ra_mtu = 0;
  356. ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
  357. if (!ndev->nd_parms) {
  358. kfree(ndev);
  359. return ERR_PTR(err);
  360. }
  361. if (ndev->cnf.forwarding)
  362. dev_disable_lro(dev);
  363. /* We refer to the device */
  364. netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL);
  365. if (snmp6_alloc_dev(ndev) < 0) {
  366. netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
  367. __func__);
  368. neigh_parms_release(&nd_tbl, ndev->nd_parms);
  369. netdev_put(dev, &ndev->dev_tracker);
  370. kfree(ndev);
  371. return ERR_PTR(err);
  372. }
  373. if (dev != blackhole_netdev) {
  374. if (snmp6_register_dev(ndev) < 0) {
  375. netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
  376. __func__, dev->name);
  377. goto err_release;
  378. }
  379. }
  380. /* One reference from device. */
  381. refcount_set(&ndev->refcnt, 1);
  382. if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
  383. ndev->cnf.accept_dad = -1;
  384. #if IS_ENABLED(CONFIG_IPV6_SIT)
  385. if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
  386. pr_info("%s: Disabled Multicast RS\n", dev->name);
  387. ndev->cnf.rtr_solicits = 0;
  388. }
  389. #endif
  390. INIT_LIST_HEAD(&ndev->tempaddr_list);
  391. ndev->desync_factor = U32_MAX;
  392. if ((dev->flags&IFF_LOOPBACK) ||
  393. dev->type == ARPHRD_TUNNEL ||
  394. dev->type == ARPHRD_TUNNEL6 ||
  395. dev->type == ARPHRD_SIT ||
  396. dev->type == ARPHRD_NONE) {
  397. ndev->cnf.use_tempaddr = -1;
  398. }
  399. ndev->token = in6addr_any;
  400. if (netif_running(dev) && addrconf_link_ready(dev))
  401. ndev->if_flags |= IF_READY;
  402. ipv6_mc_init_dev(ndev);
  403. ndev->tstamp = jiffies;
  404. if (dev != blackhole_netdev) {
  405. err = addrconf_sysctl_register(ndev);
  406. if (err) {
  407. ipv6_mc_destroy_dev(ndev);
  408. snmp6_unregister_dev(ndev);
  409. goto err_release;
  410. }
  411. }
  412. /* protected by rtnl_lock */
  413. rcu_assign_pointer(dev->ip6_ptr, ndev);
  414. if (dev != blackhole_netdev) {
  415. /* Join interface-local all-node multicast group */
  416. ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
  417. /* Join all-node multicast group */
  418. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
  419. /* Join all-router multicast group if forwarding is set */
  420. if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
  421. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
  422. }
  423. return ndev;
  424. err_release:
  425. neigh_parms_release(&nd_tbl, ndev->nd_parms);
  426. ndev->dead = 1;
  427. in6_dev_finish_destroy(ndev);
  428. return ERR_PTR(err);
  429. }
  430. static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
  431. {
  432. struct inet6_dev *idev;
  433. ASSERT_RTNL();
  434. idev = __in6_dev_get(dev);
  435. if (!idev) {
  436. idev = ipv6_add_dev(dev);
  437. if (IS_ERR(idev))
  438. return idev;
  439. }
  440. if (dev->flags&IFF_UP)
  441. ipv6_mc_up(idev);
  442. return idev;
  443. }
  444. static int inet6_netconf_msgsize_devconf(int type)
  445. {
  446. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  447. + nla_total_size(4); /* NETCONFA_IFINDEX */
  448. bool all = false;
  449. if (type == NETCONFA_ALL)
  450. all = true;
  451. if (all || type == NETCONFA_FORWARDING)
  452. size += nla_total_size(4);
  453. #ifdef CONFIG_IPV6_MROUTE
  454. if (all || type == NETCONFA_MC_FORWARDING)
  455. size += nla_total_size(4);
  456. #endif
  457. if (all || type == NETCONFA_PROXY_NEIGH)
  458. size += nla_total_size(4);
  459. if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
  460. size += nla_total_size(4);
  461. return size;
  462. }
  463. static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
  464. struct ipv6_devconf *devconf, u32 portid,
  465. u32 seq, int event, unsigned int flags,
  466. int type)
  467. {
  468. struct nlmsghdr *nlh;
  469. struct netconfmsg *ncm;
  470. bool all = false;
  471. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  472. flags);
  473. if (!nlh)
  474. return -EMSGSIZE;
  475. if (type == NETCONFA_ALL)
  476. all = true;
  477. ncm = nlmsg_data(nlh);
  478. ncm->ncm_family = AF_INET6;
  479. if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
  480. goto nla_put_failure;
  481. if (!devconf)
  482. goto out;
  483. if ((all || type == NETCONFA_FORWARDING) &&
  484. nla_put_s32(skb, NETCONFA_FORWARDING,
  485. READ_ONCE(devconf->forwarding)) < 0)
  486. goto nla_put_failure;
  487. #ifdef CONFIG_IPV6_MROUTE
  488. if ((all || type == NETCONFA_MC_FORWARDING) &&
  489. nla_put_s32(skb, NETCONFA_MC_FORWARDING,
  490. atomic_read(&devconf->mc_forwarding)) < 0)
  491. goto nla_put_failure;
  492. #endif
  493. if ((all || type == NETCONFA_PROXY_NEIGH) &&
  494. nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
  495. READ_ONCE(devconf->proxy_ndp)) < 0)
  496. goto nla_put_failure;
  497. if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
  498. nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  499. READ_ONCE(devconf->ignore_routes_with_linkdown)) < 0)
  500. goto nla_put_failure;
  501. out:
  502. nlmsg_end(skb, nlh);
  503. return 0;
  504. nla_put_failure:
  505. nlmsg_cancel(skb, nlh);
  506. return -EMSGSIZE;
  507. }
  508. void inet6_netconf_notify_devconf(struct net *net, int event, int type,
  509. int ifindex, struct ipv6_devconf *devconf)
  510. {
  511. struct sk_buff *skb;
  512. int err = -ENOBUFS;
  513. skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
  514. if (!skb)
  515. goto errout;
  516. err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
  517. event, 0, type);
  518. if (err < 0) {
  519. /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
  520. WARN_ON(err == -EMSGSIZE);
  521. kfree_skb(skb);
  522. goto errout;
  523. }
  524. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
  525. return;
  526. errout:
  527. rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
  528. }
  529. static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
  530. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  531. [NETCONFA_FORWARDING] = { .len = sizeof(int) },
  532. [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
  533. [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
  534. };
  535. static int inet6_netconf_valid_get_req(struct sk_buff *skb,
  536. const struct nlmsghdr *nlh,
  537. struct nlattr **tb,
  538. struct netlink_ext_ack *extack)
  539. {
  540. int i, err;
  541. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
  542. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
  543. return -EINVAL;
  544. }
  545. if (!netlink_strict_get_check(skb))
  546. return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
  547. tb, NETCONFA_MAX,
  548. devconf_ipv6_policy, extack);
  549. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
  550. tb, NETCONFA_MAX,
  551. devconf_ipv6_policy, extack);
  552. if (err)
  553. return err;
  554. for (i = 0; i <= NETCONFA_MAX; i++) {
  555. if (!tb[i])
  556. continue;
  557. switch (i) {
  558. case NETCONFA_IFINDEX:
  559. break;
  560. default:
  561. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
  562. return -EINVAL;
  563. }
  564. }
  565. return 0;
  566. }
  567. static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
  568. struct nlmsghdr *nlh,
  569. struct netlink_ext_ack *extack)
  570. {
  571. struct net *net = sock_net(in_skb->sk);
  572. struct nlattr *tb[NETCONFA_MAX+1];
  573. struct inet6_dev *in6_dev = NULL;
  574. struct net_device *dev = NULL;
  575. struct sk_buff *skb;
  576. struct ipv6_devconf *devconf;
  577. int ifindex;
  578. int err;
  579. err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
  580. if (err < 0)
  581. return err;
  582. if (!tb[NETCONFA_IFINDEX])
  583. return -EINVAL;
  584. err = -EINVAL;
  585. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  586. switch (ifindex) {
  587. case NETCONFA_IFINDEX_ALL:
  588. devconf = net->ipv6.devconf_all;
  589. break;
  590. case NETCONFA_IFINDEX_DEFAULT:
  591. devconf = net->ipv6.devconf_dflt;
  592. break;
  593. default:
  594. dev = dev_get_by_index(net, ifindex);
  595. if (!dev)
  596. return -EINVAL;
  597. in6_dev = in6_dev_get(dev);
  598. if (!in6_dev)
  599. goto errout;
  600. devconf = &in6_dev->cnf;
  601. break;
  602. }
  603. err = -ENOBUFS;
  604. skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  605. if (!skb)
  606. goto errout;
  607. err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
  608. NETLINK_CB(in_skb).portid,
  609. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  610. NETCONFA_ALL);
  611. if (err < 0) {
  612. /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
  613. WARN_ON(err == -EMSGSIZE);
  614. kfree_skb(skb);
  615. goto errout;
  616. }
  617. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  618. errout:
  619. if (in6_dev)
  620. in6_dev_put(in6_dev);
  621. dev_put(dev);
  622. return err;
  623. }
  624. /* Combine dev_addr_genid and dev_base_seq to detect changes.
  625. */
  626. static u32 inet6_base_seq(const struct net *net)
  627. {
  628. u32 res = atomic_read(&net->ipv6.dev_addr_genid) +
  629. READ_ONCE(net->dev_base_seq);
  630. /* Must not return 0 (see nl_dump_check_consistent()).
  631. * Chose a value far away from 0.
  632. */
  633. if (!res)
  634. res = 0x80000000;
  635. return res;
  636. }
  637. static int inet6_netconf_dump_devconf(struct sk_buff *skb,
  638. struct netlink_callback *cb)
  639. {
  640. const struct nlmsghdr *nlh = cb->nlh;
  641. struct net *net = sock_net(skb->sk);
  642. struct {
  643. unsigned long ifindex;
  644. unsigned int all_default;
  645. } *ctx = (void *)cb->ctx;
  646. struct net_device *dev;
  647. struct inet6_dev *idev;
  648. int err = 0;
  649. if (cb->strict_check) {
  650. struct netlink_ext_ack *extack = cb->extack;
  651. struct netconfmsg *ncm;
  652. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  653. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
  654. return -EINVAL;
  655. }
  656. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  657. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
  658. return -EINVAL;
  659. }
  660. }
  661. rcu_read_lock();
  662. for_each_netdev_dump(net, dev, ctx->ifindex) {
  663. idev = __in6_dev_get(dev);
  664. if (!idev)
  665. continue;
  666. err = inet6_netconf_fill_devconf(skb, dev->ifindex,
  667. &idev->cnf,
  668. NETLINK_CB(cb->skb).portid,
  669. nlh->nlmsg_seq,
  670. RTM_NEWNETCONF,
  671. NLM_F_MULTI,
  672. NETCONFA_ALL);
  673. if (err < 0)
  674. goto done;
  675. }
  676. if (ctx->all_default == 0) {
  677. err = inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
  678. net->ipv6.devconf_all,
  679. NETLINK_CB(cb->skb).portid,
  680. nlh->nlmsg_seq,
  681. RTM_NEWNETCONF, NLM_F_MULTI,
  682. NETCONFA_ALL);
  683. if (err < 0)
  684. goto done;
  685. ctx->all_default++;
  686. }
  687. if (ctx->all_default == 1) {
  688. err = inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
  689. net->ipv6.devconf_dflt,
  690. NETLINK_CB(cb->skb).portid,
  691. nlh->nlmsg_seq,
  692. RTM_NEWNETCONF, NLM_F_MULTI,
  693. NETCONFA_ALL);
  694. if (err < 0)
  695. goto done;
  696. ctx->all_default++;
  697. }
  698. done:
  699. rcu_read_unlock();
  700. return err;
  701. }
  702. #ifdef CONFIG_SYSCTL
  703. static void dev_forward_change(struct inet6_dev *idev)
  704. {
  705. struct net_device *dev;
  706. struct inet6_ifaddr *ifa;
  707. LIST_HEAD(tmp_addr_list);
  708. if (!idev)
  709. return;
  710. dev = idev->dev;
  711. if (idev->cnf.forwarding)
  712. dev_disable_lro(dev);
  713. if (dev->flags & IFF_MULTICAST) {
  714. if (idev->cnf.forwarding) {
  715. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
  716. ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
  717. ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
  718. } else {
  719. ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
  720. ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
  721. ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
  722. }
  723. }
  724. read_lock_bh(&idev->lock);
  725. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  726. if (ifa->flags&IFA_F_TENTATIVE)
  727. continue;
  728. list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
  729. }
  730. read_unlock_bh(&idev->lock);
  731. while (!list_empty(&tmp_addr_list)) {
  732. ifa = list_first_entry(&tmp_addr_list,
  733. struct inet6_ifaddr, if_list_aux);
  734. list_del(&ifa->if_list_aux);
  735. if (idev->cnf.forwarding)
  736. addrconf_join_anycast(ifa);
  737. else
  738. addrconf_leave_anycast(ifa);
  739. }
  740. inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
  741. NETCONFA_FORWARDING,
  742. dev->ifindex, &idev->cnf);
  743. }
  744. static void addrconf_forward_change(struct net *net, __s32 newf)
  745. {
  746. struct net_device *dev;
  747. struct inet6_dev *idev;
  748. for_each_netdev(net, dev) {
  749. idev = __in6_dev_get(dev);
  750. if (idev) {
  751. int changed = (!idev->cnf.forwarding) ^ (!newf);
  752. WRITE_ONCE(idev->cnf.forwarding, newf);
  753. if (changed)
  754. dev_forward_change(idev);
  755. }
  756. }
  757. }
  758. static int addrconf_fixup_forwarding(const struct ctl_table *table, int *p, int newf)
  759. {
  760. struct net *net;
  761. int old;
  762. if (!rtnl_trylock())
  763. return restart_syscall();
  764. net = (struct net *)table->extra2;
  765. old = *p;
  766. WRITE_ONCE(*p, newf);
  767. if (p == &net->ipv6.devconf_dflt->forwarding) {
  768. if ((!newf) ^ (!old))
  769. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  770. NETCONFA_FORWARDING,
  771. NETCONFA_IFINDEX_DEFAULT,
  772. net->ipv6.devconf_dflt);
  773. rtnl_unlock();
  774. return 0;
  775. }
  776. if (p == &net->ipv6.devconf_all->forwarding) {
  777. int old_dflt = net->ipv6.devconf_dflt->forwarding;
  778. WRITE_ONCE(net->ipv6.devconf_dflt->forwarding, newf);
  779. if ((!newf) ^ (!old_dflt))
  780. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  781. NETCONFA_FORWARDING,
  782. NETCONFA_IFINDEX_DEFAULT,
  783. net->ipv6.devconf_dflt);
  784. addrconf_forward_change(net, newf);
  785. if ((!newf) ^ (!old))
  786. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  787. NETCONFA_FORWARDING,
  788. NETCONFA_IFINDEX_ALL,
  789. net->ipv6.devconf_all);
  790. } else if ((!newf) ^ (!old))
  791. dev_forward_change((struct inet6_dev *)table->extra1);
  792. rtnl_unlock();
  793. if (newf)
  794. rt6_purge_dflt_routers(net);
  795. return 1;
  796. }
  797. static void addrconf_linkdown_change(struct net *net, __s32 newf)
  798. {
  799. struct net_device *dev;
  800. struct inet6_dev *idev;
  801. for_each_netdev(net, dev) {
  802. idev = __in6_dev_get(dev);
  803. if (idev) {
  804. int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
  805. WRITE_ONCE(idev->cnf.ignore_routes_with_linkdown, newf);
  806. if (changed)
  807. inet6_netconf_notify_devconf(dev_net(dev),
  808. RTM_NEWNETCONF,
  809. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  810. dev->ifindex,
  811. &idev->cnf);
  812. }
  813. }
  814. }
  815. static int addrconf_fixup_linkdown(const struct ctl_table *table, int *p, int newf)
  816. {
  817. struct net *net;
  818. int old;
  819. if (!rtnl_trylock())
  820. return restart_syscall();
  821. net = (struct net *)table->extra2;
  822. old = *p;
  823. WRITE_ONCE(*p, newf);
  824. if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
  825. if ((!newf) ^ (!old))
  826. inet6_netconf_notify_devconf(net,
  827. RTM_NEWNETCONF,
  828. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  829. NETCONFA_IFINDEX_DEFAULT,
  830. net->ipv6.devconf_dflt);
  831. rtnl_unlock();
  832. return 0;
  833. }
  834. if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
  835. WRITE_ONCE(net->ipv6.devconf_dflt->ignore_routes_with_linkdown, newf);
  836. addrconf_linkdown_change(net, newf);
  837. if ((!newf) ^ (!old))
  838. inet6_netconf_notify_devconf(net,
  839. RTM_NEWNETCONF,
  840. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  841. NETCONFA_IFINDEX_ALL,
  842. net->ipv6.devconf_all);
  843. }
  844. rtnl_unlock();
  845. return 1;
  846. }
  847. #endif
  848. /* Nobody refers to this ifaddr, destroy it */
  849. void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
  850. {
  851. WARN_ON(!hlist_unhashed(&ifp->addr_lst));
  852. #ifdef NET_REFCNT_DEBUG
  853. pr_debug("%s\n", __func__);
  854. #endif
  855. in6_dev_put(ifp->idev);
  856. if (cancel_delayed_work(&ifp->dad_work))
  857. pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
  858. ifp);
  859. if (ifp->state != INET6_IFADDR_STATE_DEAD) {
  860. pr_warn("Freeing alive inet6 address %p\n", ifp);
  861. return;
  862. }
  863. kfree_rcu(ifp, rcu);
  864. }
  865. static void
  866. ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
  867. {
  868. struct list_head *p;
  869. int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
  870. /*
  871. * Each device address list is sorted in order of scope -
  872. * global before linklocal.
  873. */
  874. list_for_each(p, &idev->addr_list) {
  875. struct inet6_ifaddr *ifa
  876. = list_entry(p, struct inet6_ifaddr, if_list);
  877. if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
  878. break;
  879. }
  880. list_add_tail_rcu(&ifp->if_list, p);
  881. }
  882. static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
  883. {
  884. u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
  885. return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
  886. }
  887. static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
  888. struct net_device *dev, unsigned int hash)
  889. {
  890. struct inet6_ifaddr *ifp;
  891. hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
  892. if (ipv6_addr_equal(&ifp->addr, addr)) {
  893. if (!dev || ifp->idev->dev == dev)
  894. return true;
  895. }
  896. }
  897. return false;
  898. }
  899. static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
  900. {
  901. struct net *net = dev_net(dev);
  902. unsigned int hash = inet6_addr_hash(net, &ifa->addr);
  903. int err = 0;
  904. spin_lock_bh(&net->ipv6.addrconf_hash_lock);
  905. /* Ignore adding duplicate addresses on an interface */
  906. if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) {
  907. netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
  908. err = -EEXIST;
  909. } else {
  910. hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]);
  911. }
  912. spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
  913. return err;
  914. }
  915. /* On success it returns ifp with increased reference count */
  916. static struct inet6_ifaddr *
  917. ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
  918. bool can_block, struct netlink_ext_ack *extack)
  919. {
  920. gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
  921. int addr_type = ipv6_addr_type(cfg->pfx);
  922. struct net *net = dev_net(idev->dev);
  923. struct inet6_ifaddr *ifa = NULL;
  924. struct fib6_info *f6i = NULL;
  925. int err = 0;
  926. if (addr_type == IPV6_ADDR_ANY) {
  927. NL_SET_ERR_MSG_MOD(extack, "Invalid address");
  928. return ERR_PTR(-EADDRNOTAVAIL);
  929. } else if (addr_type & IPV6_ADDR_MULTICAST &&
  930. !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) {
  931. NL_SET_ERR_MSG_MOD(extack, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag");
  932. return ERR_PTR(-EADDRNOTAVAIL);
  933. } else if (!(idev->dev->flags & IFF_LOOPBACK) &&
  934. !netif_is_l3_master(idev->dev) &&
  935. addr_type & IPV6_ADDR_LOOPBACK) {
  936. NL_SET_ERR_MSG_MOD(extack, "Cannot assign loopback address on this device");
  937. return ERR_PTR(-EADDRNOTAVAIL);
  938. }
  939. if (idev->dead) {
  940. NL_SET_ERR_MSG_MOD(extack, "device is going away");
  941. err = -ENODEV;
  942. goto out;
  943. }
  944. if (idev->cnf.disable_ipv6) {
  945. NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
  946. err = -EACCES;
  947. goto out;
  948. }
  949. /* validator notifier needs to be blocking;
  950. * do not call in atomic context
  951. */
  952. if (can_block) {
  953. struct in6_validator_info i6vi = {
  954. .i6vi_addr = *cfg->pfx,
  955. .i6vi_dev = idev,
  956. .extack = extack,
  957. };
  958. err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
  959. err = notifier_to_errno(err);
  960. if (err < 0)
  961. goto out;
  962. }
  963. ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
  964. if (!ifa) {
  965. err = -ENOBUFS;
  966. goto out;
  967. }
  968. f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags, extack);
  969. if (IS_ERR(f6i)) {
  970. err = PTR_ERR(f6i);
  971. f6i = NULL;
  972. goto out;
  973. }
  974. neigh_parms_data_state_setall(idev->nd_parms);
  975. ifa->addr = *cfg->pfx;
  976. if (cfg->peer_pfx)
  977. ifa->peer_addr = *cfg->peer_pfx;
  978. spin_lock_init(&ifa->lock);
  979. INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
  980. INIT_HLIST_NODE(&ifa->addr_lst);
  981. ifa->scope = cfg->scope;
  982. ifa->prefix_len = cfg->plen;
  983. ifa->rt_priority = cfg->rt_priority;
  984. ifa->flags = cfg->ifa_flags;
  985. ifa->ifa_proto = cfg->ifa_proto;
  986. /* No need to add the TENTATIVE flag for addresses with NODAD */
  987. if (!(cfg->ifa_flags & IFA_F_NODAD))
  988. ifa->flags |= IFA_F_TENTATIVE;
  989. ifa->valid_lft = cfg->valid_lft;
  990. ifa->prefered_lft = cfg->preferred_lft;
  991. ifa->cstamp = ifa->tstamp = jiffies;
  992. ifa->tokenized = false;
  993. ifa->rt = f6i;
  994. ifa->idev = idev;
  995. in6_dev_hold(idev);
  996. /* For caller */
  997. refcount_set(&ifa->refcnt, 1);
  998. rcu_read_lock();
  999. err = ipv6_add_addr_hash(idev->dev, ifa);
  1000. if (err < 0) {
  1001. rcu_read_unlock();
  1002. goto out;
  1003. }
  1004. write_lock_bh(&idev->lock);
  1005. /* Add to inet6_dev unicast addr list. */
  1006. ipv6_link_dev_addr(idev, ifa);
  1007. if (ifa->flags&IFA_F_TEMPORARY) {
  1008. list_add(&ifa->tmp_list, &idev->tempaddr_list);
  1009. in6_ifa_hold(ifa);
  1010. }
  1011. in6_ifa_hold(ifa);
  1012. write_unlock_bh(&idev->lock);
  1013. rcu_read_unlock();
  1014. inet6addr_notifier_call_chain(NETDEV_UP, ifa);
  1015. out:
  1016. if (unlikely(err < 0)) {
  1017. fib6_info_release(f6i);
  1018. if (ifa) {
  1019. if (ifa->idev)
  1020. in6_dev_put(ifa->idev);
  1021. kfree(ifa);
  1022. }
  1023. ifa = ERR_PTR(err);
  1024. }
  1025. return ifa;
  1026. }
  1027. enum cleanup_prefix_rt_t {
  1028. CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
  1029. CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
  1030. CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
  1031. };
  1032. /*
  1033. * Check, whether the prefix for ifp would still need a prefix route
  1034. * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
  1035. * constants.
  1036. *
  1037. * 1) we don't purge prefix if address was not permanent.
  1038. * prefix is managed by its own lifetime.
  1039. * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
  1040. * 3) if there are no addresses, delete prefix.
  1041. * 4) if there are still other permanent address(es),
  1042. * corresponding prefix is still permanent.
  1043. * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
  1044. * don't purge the prefix, assume user space is managing it.
  1045. * 6) otherwise, update prefix lifetime to the
  1046. * longest valid lifetime among the corresponding
  1047. * addresses on the device.
  1048. * Note: subsequent RA will update lifetime.
  1049. **/
  1050. static enum cleanup_prefix_rt_t
  1051. check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
  1052. {
  1053. struct inet6_ifaddr *ifa;
  1054. struct inet6_dev *idev = ifp->idev;
  1055. unsigned long lifetime;
  1056. enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
  1057. *expires = jiffies;
  1058. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  1059. if (ifa == ifp)
  1060. continue;
  1061. if (ifa->prefix_len != ifp->prefix_len ||
  1062. !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
  1063. ifp->prefix_len))
  1064. continue;
  1065. if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
  1066. return CLEANUP_PREFIX_RT_NOP;
  1067. action = CLEANUP_PREFIX_RT_EXPIRE;
  1068. spin_lock(&ifa->lock);
  1069. lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
  1070. /*
  1071. * Note: Because this address is
  1072. * not permanent, lifetime <
  1073. * LONG_MAX / HZ here.
  1074. */
  1075. if (time_before(*expires, ifa->tstamp + lifetime * HZ))
  1076. *expires = ifa->tstamp + lifetime * HZ;
  1077. spin_unlock(&ifa->lock);
  1078. }
  1079. return action;
  1080. }
  1081. static void
  1082. cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
  1083. bool del_rt, bool del_peer)
  1084. {
  1085. struct fib6_table *table;
  1086. struct fib6_info *f6i;
  1087. f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
  1088. ifp->prefix_len,
  1089. ifp->idev->dev, 0, RTF_DEFAULT, true);
  1090. if (f6i) {
  1091. if (del_rt)
  1092. ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
  1093. else {
  1094. if (!(f6i->fib6_flags & RTF_EXPIRES)) {
  1095. table = f6i->fib6_table;
  1096. spin_lock_bh(&table->tb6_lock);
  1097. fib6_set_expires(f6i, expires);
  1098. fib6_add_gc_list(f6i);
  1099. spin_unlock_bh(&table->tb6_lock);
  1100. }
  1101. fib6_info_release(f6i);
  1102. }
  1103. }
  1104. }
  1105. /* This function wants to get referenced ifp and releases it before return */
  1106. static void ipv6_del_addr(struct inet6_ifaddr *ifp)
  1107. {
  1108. enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
  1109. struct net *net = dev_net(ifp->idev->dev);
  1110. unsigned long expires;
  1111. int state;
  1112. ASSERT_RTNL();
  1113. spin_lock_bh(&ifp->lock);
  1114. state = ifp->state;
  1115. ifp->state = INET6_IFADDR_STATE_DEAD;
  1116. spin_unlock_bh(&ifp->lock);
  1117. if (state == INET6_IFADDR_STATE_DEAD)
  1118. goto out;
  1119. spin_lock_bh(&net->ipv6.addrconf_hash_lock);
  1120. hlist_del_init_rcu(&ifp->addr_lst);
  1121. spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
  1122. write_lock_bh(&ifp->idev->lock);
  1123. if (ifp->flags&IFA_F_TEMPORARY) {
  1124. list_del(&ifp->tmp_list);
  1125. if (ifp->ifpub) {
  1126. in6_ifa_put(ifp->ifpub);
  1127. ifp->ifpub = NULL;
  1128. }
  1129. __in6_ifa_put(ifp);
  1130. }
  1131. if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
  1132. action = check_cleanup_prefix_route(ifp, &expires);
  1133. list_del_rcu(&ifp->if_list);
  1134. __in6_ifa_put(ifp);
  1135. write_unlock_bh(&ifp->idev->lock);
  1136. addrconf_del_dad_work(ifp);
  1137. ipv6_ifa_notify(RTM_DELADDR, ifp);
  1138. inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
  1139. if (action != CLEANUP_PREFIX_RT_NOP) {
  1140. cleanup_prefix_route(ifp, expires,
  1141. action == CLEANUP_PREFIX_RT_DEL, false);
  1142. }
  1143. /* clean up prefsrc entries */
  1144. rt6_remove_prefsrc(ifp);
  1145. out:
  1146. in6_ifa_put(ifp);
  1147. }
  1148. static unsigned long ipv6_get_regen_advance(const struct inet6_dev *idev)
  1149. {
  1150. return READ_ONCE(idev->cnf.regen_min_advance) +
  1151. READ_ONCE(idev->cnf.regen_max_retry) *
  1152. READ_ONCE(idev->cnf.dad_transmits) *
  1153. max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
  1154. }
  1155. static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
  1156. {
  1157. struct inet6_dev *idev = ifp->idev;
  1158. unsigned long tmp_tstamp, age;
  1159. unsigned long regen_advance;
  1160. unsigned long now = jiffies;
  1161. u32 if_public_preferred_lft;
  1162. s32 cnf_temp_preferred_lft;
  1163. struct inet6_ifaddr *ift;
  1164. struct ifa6_config cfg;
  1165. long max_desync_factor;
  1166. struct in6_addr addr;
  1167. int ret = 0;
  1168. write_lock_bh(&idev->lock);
  1169. retry:
  1170. in6_dev_hold(idev);
  1171. if (READ_ONCE(idev->cnf.use_tempaddr) <= 0) {
  1172. write_unlock_bh(&idev->lock);
  1173. pr_info("%s: use_tempaddr is disabled\n", __func__);
  1174. in6_dev_put(idev);
  1175. ret = -1;
  1176. goto out;
  1177. }
  1178. spin_lock_bh(&ifp->lock);
  1179. if (ifp->regen_count++ >= READ_ONCE(idev->cnf.regen_max_retry)) {
  1180. WRITE_ONCE(idev->cnf.use_tempaddr, -1); /*XXX*/
  1181. spin_unlock_bh(&ifp->lock);
  1182. write_unlock_bh(&idev->lock);
  1183. pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
  1184. __func__);
  1185. in6_dev_put(idev);
  1186. ret = -1;
  1187. goto out;
  1188. }
  1189. in6_ifa_hold(ifp);
  1190. memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
  1191. ipv6_gen_rnd_iid(&addr);
  1192. age = (now - ifp->tstamp) / HZ;
  1193. regen_advance = ipv6_get_regen_advance(idev);
  1194. /* recalculate max_desync_factor each time and update
  1195. * idev->desync_factor if it's larger
  1196. */
  1197. cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
  1198. max_desync_factor = min_t(long,
  1199. READ_ONCE(idev->cnf.max_desync_factor),
  1200. cnf_temp_preferred_lft - regen_advance);
  1201. if (unlikely(idev->desync_factor > max_desync_factor)) {
  1202. if (max_desync_factor > 0) {
  1203. get_random_bytes(&idev->desync_factor,
  1204. sizeof(idev->desync_factor));
  1205. idev->desync_factor %= max_desync_factor;
  1206. } else {
  1207. idev->desync_factor = 0;
  1208. }
  1209. }
  1210. if_public_preferred_lft = ifp->prefered_lft;
  1211. memset(&cfg, 0, sizeof(cfg));
  1212. cfg.valid_lft = min_t(__u32, ifp->valid_lft,
  1213. READ_ONCE(idev->cnf.temp_valid_lft) + age);
  1214. cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
  1215. cfg.preferred_lft = min_t(__u32, if_public_preferred_lft, cfg.preferred_lft);
  1216. cfg.preferred_lft = min_t(__u32, cfg.valid_lft, cfg.preferred_lft);
  1217. cfg.plen = ifp->prefix_len;
  1218. tmp_tstamp = ifp->tstamp;
  1219. spin_unlock_bh(&ifp->lock);
  1220. write_unlock_bh(&idev->lock);
  1221. /* From RFC 4941:
  1222. *
  1223. * A temporary address is created only if this calculated Preferred
  1224. * Lifetime is greater than REGEN_ADVANCE time units. In
  1225. * particular, an implementation must not create a temporary address
  1226. * with a zero Preferred Lifetime.
  1227. *
  1228. * ...
  1229. *
  1230. * When creating a temporary address, the lifetime values MUST be
  1231. * derived from the corresponding prefix as follows:
  1232. *
  1233. * ...
  1234. *
  1235. * * Its Preferred Lifetime is the lower of the Preferred Lifetime
  1236. * of the public address or TEMP_PREFERRED_LIFETIME -
  1237. * DESYNC_FACTOR.
  1238. *
  1239. * To comply with the RFC's requirements, clamp the preferred lifetime
  1240. * to a minimum of regen_advance, unless that would exceed valid_lft or
  1241. * ifp->prefered_lft.
  1242. *
  1243. * Use age calculation as in addrconf_verify to avoid unnecessary
  1244. * temporary addresses being generated.
  1245. */
  1246. age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  1247. if (cfg.preferred_lft <= regen_advance + age) {
  1248. cfg.preferred_lft = regen_advance + age + 1;
  1249. if (cfg.preferred_lft > cfg.valid_lft ||
  1250. cfg.preferred_lft > if_public_preferred_lft) {
  1251. in6_ifa_put(ifp);
  1252. in6_dev_put(idev);
  1253. ret = -1;
  1254. goto out;
  1255. }
  1256. }
  1257. cfg.ifa_flags = IFA_F_TEMPORARY;
  1258. /* set in addrconf_prefix_rcv() */
  1259. if (ifp->flags & IFA_F_OPTIMISTIC)
  1260. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  1261. cfg.pfx = &addr;
  1262. cfg.scope = ipv6_addr_scope(cfg.pfx);
  1263. ift = ipv6_add_addr(idev, &cfg, block, NULL);
  1264. if (IS_ERR(ift)) {
  1265. in6_ifa_put(ifp);
  1266. in6_dev_put(idev);
  1267. pr_info("%s: retry temporary address regeneration\n", __func__);
  1268. write_lock_bh(&idev->lock);
  1269. goto retry;
  1270. }
  1271. spin_lock_bh(&ift->lock);
  1272. ift->ifpub = ifp;
  1273. ift->cstamp = now;
  1274. ift->tstamp = tmp_tstamp;
  1275. spin_unlock_bh(&ift->lock);
  1276. addrconf_dad_start(ift);
  1277. in6_ifa_put(ift);
  1278. in6_dev_put(idev);
  1279. out:
  1280. return ret;
  1281. }
  1282. /*
  1283. * Choose an appropriate source address (RFC3484)
  1284. */
  1285. enum {
  1286. IPV6_SADDR_RULE_INIT = 0,
  1287. IPV6_SADDR_RULE_LOCAL,
  1288. IPV6_SADDR_RULE_SCOPE,
  1289. IPV6_SADDR_RULE_PREFERRED,
  1290. #ifdef CONFIG_IPV6_MIP6
  1291. IPV6_SADDR_RULE_HOA,
  1292. #endif
  1293. IPV6_SADDR_RULE_OIF,
  1294. IPV6_SADDR_RULE_LABEL,
  1295. IPV6_SADDR_RULE_PRIVACY,
  1296. IPV6_SADDR_RULE_ORCHID,
  1297. IPV6_SADDR_RULE_PREFIX,
  1298. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1299. IPV6_SADDR_RULE_NOT_OPTIMISTIC,
  1300. #endif
  1301. IPV6_SADDR_RULE_MAX
  1302. };
  1303. struct ipv6_saddr_score {
  1304. int rule;
  1305. int addr_type;
  1306. struct inet6_ifaddr *ifa;
  1307. DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
  1308. int scopedist;
  1309. int matchlen;
  1310. };
  1311. struct ipv6_saddr_dst {
  1312. const struct in6_addr *addr;
  1313. int ifindex;
  1314. int scope;
  1315. int label;
  1316. unsigned int prefs;
  1317. };
  1318. static inline int ipv6_saddr_preferred(int type)
  1319. {
  1320. if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
  1321. return 1;
  1322. return 0;
  1323. }
  1324. static bool ipv6_use_optimistic_addr(const struct net *net,
  1325. const struct inet6_dev *idev)
  1326. {
  1327. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1328. if (!idev)
  1329. return false;
  1330. if (!READ_ONCE(net->ipv6.devconf_all->optimistic_dad) &&
  1331. !READ_ONCE(idev->cnf.optimistic_dad))
  1332. return false;
  1333. if (!READ_ONCE(net->ipv6.devconf_all->use_optimistic) &&
  1334. !READ_ONCE(idev->cnf.use_optimistic))
  1335. return false;
  1336. return true;
  1337. #else
  1338. return false;
  1339. #endif
  1340. }
  1341. static bool ipv6_allow_optimistic_dad(const struct net *net,
  1342. const struct inet6_dev *idev)
  1343. {
  1344. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1345. if (!idev)
  1346. return false;
  1347. if (!READ_ONCE(net->ipv6.devconf_all->optimistic_dad) &&
  1348. !READ_ONCE(idev->cnf.optimistic_dad))
  1349. return false;
  1350. return true;
  1351. #else
  1352. return false;
  1353. #endif
  1354. }
  1355. static int ipv6_get_saddr_eval(struct net *net,
  1356. struct ipv6_saddr_score *score,
  1357. struct ipv6_saddr_dst *dst,
  1358. int i)
  1359. {
  1360. int ret;
  1361. if (i <= score->rule) {
  1362. switch (i) {
  1363. case IPV6_SADDR_RULE_SCOPE:
  1364. ret = score->scopedist;
  1365. break;
  1366. case IPV6_SADDR_RULE_PREFIX:
  1367. ret = score->matchlen;
  1368. break;
  1369. default:
  1370. ret = !!test_bit(i, score->scorebits);
  1371. }
  1372. goto out;
  1373. }
  1374. switch (i) {
  1375. case IPV6_SADDR_RULE_INIT:
  1376. /* Rule 0: remember if hiscore is not ready yet */
  1377. ret = !!score->ifa;
  1378. break;
  1379. case IPV6_SADDR_RULE_LOCAL:
  1380. /* Rule 1: Prefer same address */
  1381. ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
  1382. break;
  1383. case IPV6_SADDR_RULE_SCOPE:
  1384. /* Rule 2: Prefer appropriate scope
  1385. *
  1386. * ret
  1387. * ^
  1388. * -1 | d 15
  1389. * ---+--+-+---> scope
  1390. * |
  1391. * | d is scope of the destination.
  1392. * B-d | \
  1393. * | \ <- smaller scope is better if
  1394. * B-15 | \ if scope is enough for destination.
  1395. * | ret = B - scope (-1 <= scope >= d <= 15).
  1396. * d-C-1 | /
  1397. * |/ <- greater is better
  1398. * -C / if scope is not enough for destination.
  1399. * /| ret = scope - C (-1 <= d < scope <= 15).
  1400. *
  1401. * d - C - 1 < B -15 (for all -1 <= d <= 15).
  1402. * C > d + 14 - B >= 15 + 14 - B = 29 - B.
  1403. * Assume B = 0 and we get C > 29.
  1404. */
  1405. ret = __ipv6_addr_src_scope(score->addr_type);
  1406. if (ret >= dst->scope)
  1407. ret = -ret;
  1408. else
  1409. ret -= 128; /* 30 is enough */
  1410. score->scopedist = ret;
  1411. break;
  1412. case IPV6_SADDR_RULE_PREFERRED:
  1413. {
  1414. /* Rule 3: Avoid deprecated and optimistic addresses */
  1415. u8 avoid = IFA_F_DEPRECATED;
  1416. if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
  1417. avoid |= IFA_F_OPTIMISTIC;
  1418. ret = ipv6_saddr_preferred(score->addr_type) ||
  1419. !(score->ifa->flags & avoid);
  1420. break;
  1421. }
  1422. #ifdef CONFIG_IPV6_MIP6
  1423. case IPV6_SADDR_RULE_HOA:
  1424. {
  1425. /* Rule 4: Prefer home address */
  1426. int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
  1427. ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
  1428. break;
  1429. }
  1430. #endif
  1431. case IPV6_SADDR_RULE_OIF:
  1432. /* Rule 5: Prefer outgoing interface */
  1433. ret = (!dst->ifindex ||
  1434. dst->ifindex == score->ifa->idev->dev->ifindex);
  1435. break;
  1436. case IPV6_SADDR_RULE_LABEL:
  1437. /* Rule 6: Prefer matching label */
  1438. ret = ipv6_addr_label(net,
  1439. &score->ifa->addr, score->addr_type,
  1440. score->ifa->idev->dev->ifindex) == dst->label;
  1441. break;
  1442. case IPV6_SADDR_RULE_PRIVACY:
  1443. {
  1444. /* Rule 7: Prefer public address
  1445. * Note: prefer temporary address if use_tempaddr >= 2
  1446. */
  1447. int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
  1448. !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
  1449. READ_ONCE(score->ifa->idev->cnf.use_tempaddr) >= 2;
  1450. ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
  1451. break;
  1452. }
  1453. case IPV6_SADDR_RULE_ORCHID:
  1454. /* Rule 8-: Prefer ORCHID vs ORCHID or
  1455. * non-ORCHID vs non-ORCHID
  1456. */
  1457. ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
  1458. ipv6_addr_orchid(dst->addr));
  1459. break;
  1460. case IPV6_SADDR_RULE_PREFIX:
  1461. /* Rule 8: Use longest matching prefix */
  1462. ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
  1463. if (ret > score->ifa->prefix_len)
  1464. ret = score->ifa->prefix_len;
  1465. score->matchlen = ret;
  1466. break;
  1467. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1468. case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
  1469. /* Optimistic addresses still have lower precedence than other
  1470. * preferred addresses.
  1471. */
  1472. ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
  1473. break;
  1474. #endif
  1475. default:
  1476. ret = 0;
  1477. }
  1478. if (ret)
  1479. __set_bit(i, score->scorebits);
  1480. score->rule = i;
  1481. out:
  1482. return ret;
  1483. }
  1484. static int __ipv6_dev_get_saddr(struct net *net,
  1485. struct ipv6_saddr_dst *dst,
  1486. struct inet6_dev *idev,
  1487. struct ipv6_saddr_score *scores,
  1488. int hiscore_idx)
  1489. {
  1490. struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
  1491. list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
  1492. int i;
  1493. /*
  1494. * - Tentative Address (RFC2462 section 5.4)
  1495. * - A tentative address is not considered
  1496. * "assigned to an interface" in the traditional
  1497. * sense, unless it is also flagged as optimistic.
  1498. * - Candidate Source Address (section 4)
  1499. * - In any case, anycast addresses, multicast
  1500. * addresses, and the unspecified address MUST
  1501. * NOT be included in a candidate set.
  1502. */
  1503. if ((score->ifa->flags & IFA_F_TENTATIVE) &&
  1504. (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
  1505. continue;
  1506. score->addr_type = __ipv6_addr_type(&score->ifa->addr);
  1507. if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
  1508. score->addr_type & IPV6_ADDR_MULTICAST)) {
  1509. net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
  1510. idev->dev->name);
  1511. continue;
  1512. }
  1513. score->rule = -1;
  1514. bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
  1515. for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
  1516. int minihiscore, miniscore;
  1517. minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
  1518. miniscore = ipv6_get_saddr_eval(net, score, dst, i);
  1519. if (minihiscore > miniscore) {
  1520. if (i == IPV6_SADDR_RULE_SCOPE &&
  1521. score->scopedist > 0) {
  1522. /*
  1523. * special case:
  1524. * each remaining entry
  1525. * has too small (not enough)
  1526. * scope, because ifa entries
  1527. * are sorted by their scope
  1528. * values.
  1529. */
  1530. goto out;
  1531. }
  1532. break;
  1533. } else if (minihiscore < miniscore) {
  1534. swap(hiscore, score);
  1535. hiscore_idx = 1 - hiscore_idx;
  1536. /* restore our iterator */
  1537. score->ifa = hiscore->ifa;
  1538. break;
  1539. }
  1540. }
  1541. }
  1542. out:
  1543. return hiscore_idx;
  1544. }
  1545. static int ipv6_get_saddr_master(struct net *net,
  1546. const struct net_device *dst_dev,
  1547. const struct net_device *master,
  1548. struct ipv6_saddr_dst *dst,
  1549. struct ipv6_saddr_score *scores,
  1550. int hiscore_idx)
  1551. {
  1552. struct inet6_dev *idev;
  1553. idev = __in6_dev_get(dst_dev);
  1554. if (idev)
  1555. hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
  1556. scores, hiscore_idx);
  1557. idev = __in6_dev_get(master);
  1558. if (idev)
  1559. hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
  1560. scores, hiscore_idx);
  1561. return hiscore_idx;
  1562. }
  1563. int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
  1564. const struct in6_addr *daddr, unsigned int prefs,
  1565. struct in6_addr *saddr)
  1566. {
  1567. struct ipv6_saddr_score scores[2], *hiscore;
  1568. struct ipv6_saddr_dst dst;
  1569. struct inet6_dev *idev;
  1570. struct net_device *dev;
  1571. int dst_type;
  1572. bool use_oif_addr = false;
  1573. int hiscore_idx = 0;
  1574. int ret = 0;
  1575. dst_type = __ipv6_addr_type(daddr);
  1576. dst.addr = daddr;
  1577. dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
  1578. dst.scope = __ipv6_addr_src_scope(dst_type);
  1579. dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
  1580. dst.prefs = prefs;
  1581. scores[hiscore_idx].rule = -1;
  1582. scores[hiscore_idx].ifa = NULL;
  1583. rcu_read_lock();
  1584. /* Candidate Source Address (section 4)
  1585. * - multicast and link-local destination address,
  1586. * the set of candidate source address MUST only
  1587. * include addresses assigned to interfaces
  1588. * belonging to the same link as the outgoing
  1589. * interface.
  1590. * (- For site-local destination addresses, the
  1591. * set of candidate source addresses MUST only
  1592. * include addresses assigned to interfaces
  1593. * belonging to the same site as the outgoing
  1594. * interface.)
  1595. * - "It is RECOMMENDED that the candidate source addresses
  1596. * be the set of unicast addresses assigned to the
  1597. * interface that will be used to send to the destination
  1598. * (the 'outgoing' interface)." (RFC 6724)
  1599. */
  1600. if (dst_dev) {
  1601. idev = __in6_dev_get(dst_dev);
  1602. if ((dst_type & IPV6_ADDR_MULTICAST) ||
  1603. dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
  1604. (idev && READ_ONCE(idev->cnf.use_oif_addrs_only))) {
  1605. use_oif_addr = true;
  1606. }
  1607. }
  1608. if (use_oif_addr) {
  1609. if (idev)
  1610. hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
  1611. } else {
  1612. const struct net_device *master;
  1613. int master_idx = 0;
  1614. /* if dst_dev exists and is enslaved to an L3 device, then
  1615. * prefer addresses from dst_dev and then the master over
  1616. * any other enslaved devices in the L3 domain.
  1617. */
  1618. master = l3mdev_master_dev_rcu(dst_dev);
  1619. if (master) {
  1620. master_idx = master->ifindex;
  1621. hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
  1622. master, &dst,
  1623. scores, hiscore_idx);
  1624. if (scores[hiscore_idx].ifa &&
  1625. scores[hiscore_idx].scopedist >= 0)
  1626. goto out;
  1627. }
  1628. for_each_netdev_rcu(net, dev) {
  1629. /* only consider addresses on devices in the
  1630. * same L3 domain
  1631. */
  1632. if (l3mdev_master_ifindex_rcu(dev) != master_idx)
  1633. continue;
  1634. idev = __in6_dev_get(dev);
  1635. if (!idev)
  1636. continue;
  1637. hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
  1638. }
  1639. }
  1640. out:
  1641. hiscore = &scores[hiscore_idx];
  1642. if (!hiscore->ifa)
  1643. ret = -EADDRNOTAVAIL;
  1644. else
  1645. *saddr = hiscore->ifa->addr;
  1646. rcu_read_unlock();
  1647. return ret;
  1648. }
  1649. EXPORT_SYMBOL(ipv6_dev_get_saddr);
  1650. static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
  1651. u32 banned_flags)
  1652. {
  1653. struct inet6_ifaddr *ifp;
  1654. int err = -EADDRNOTAVAIL;
  1655. list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
  1656. if (ifp->scope > IFA_LINK)
  1657. break;
  1658. if (ifp->scope == IFA_LINK &&
  1659. !(ifp->flags & banned_flags)) {
  1660. *addr = ifp->addr;
  1661. err = 0;
  1662. break;
  1663. }
  1664. }
  1665. return err;
  1666. }
  1667. int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
  1668. u32 banned_flags)
  1669. {
  1670. struct inet6_dev *idev;
  1671. int err = -EADDRNOTAVAIL;
  1672. rcu_read_lock();
  1673. idev = __in6_dev_get(dev);
  1674. if (idev) {
  1675. read_lock_bh(&idev->lock);
  1676. err = __ipv6_get_lladdr(idev, addr, banned_flags);
  1677. read_unlock_bh(&idev->lock);
  1678. }
  1679. rcu_read_unlock();
  1680. return err;
  1681. }
  1682. static int ipv6_count_addresses(const struct inet6_dev *idev)
  1683. {
  1684. const struct inet6_ifaddr *ifp;
  1685. int cnt = 0;
  1686. rcu_read_lock();
  1687. list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
  1688. cnt++;
  1689. rcu_read_unlock();
  1690. return cnt;
  1691. }
  1692. int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
  1693. const struct net_device *dev, int strict)
  1694. {
  1695. return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
  1696. strict, IFA_F_TENTATIVE);
  1697. }
  1698. EXPORT_SYMBOL(ipv6_chk_addr);
  1699. /* device argument is used to find the L3 domain of interest. If
  1700. * skip_dev_check is set, then the ifp device is not checked against
  1701. * the passed in dev argument. So the 2 cases for addresses checks are:
  1702. * 1. does the address exist in the L3 domain that dev is part of
  1703. * (skip_dev_check = true), or
  1704. *
  1705. * 2. does the address exist on the specific device
  1706. * (skip_dev_check = false)
  1707. */
  1708. static struct net_device *
  1709. __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
  1710. const struct net_device *dev, bool skip_dev_check,
  1711. int strict, u32 banned_flags)
  1712. {
  1713. unsigned int hash = inet6_addr_hash(net, addr);
  1714. struct net_device *l3mdev, *ndev;
  1715. struct inet6_ifaddr *ifp;
  1716. u32 ifp_flags;
  1717. rcu_read_lock();
  1718. l3mdev = l3mdev_master_dev_rcu(dev);
  1719. if (skip_dev_check)
  1720. dev = NULL;
  1721. hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
  1722. ndev = ifp->idev->dev;
  1723. if (l3mdev_master_dev_rcu(ndev) != l3mdev)
  1724. continue;
  1725. /* Decouple optimistic from tentative for evaluation here.
  1726. * Ban optimistic addresses explicitly, when required.
  1727. */
  1728. ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
  1729. ? (ifp->flags&~IFA_F_TENTATIVE)
  1730. : ifp->flags;
  1731. if (ipv6_addr_equal(&ifp->addr, addr) &&
  1732. !(ifp_flags&banned_flags) &&
  1733. (!dev || ndev == dev ||
  1734. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
  1735. rcu_read_unlock();
  1736. return ndev;
  1737. }
  1738. }
  1739. rcu_read_unlock();
  1740. return NULL;
  1741. }
  1742. int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
  1743. const struct net_device *dev, bool skip_dev_check,
  1744. int strict, u32 banned_flags)
  1745. {
  1746. return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
  1747. strict, banned_flags) ? 1 : 0;
  1748. }
  1749. EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
  1750. /* Compares an address/prefix_len with addresses on device @dev.
  1751. * If one is found it returns true.
  1752. */
  1753. bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
  1754. const unsigned int prefix_len, struct net_device *dev)
  1755. {
  1756. const struct inet6_ifaddr *ifa;
  1757. const struct inet6_dev *idev;
  1758. bool ret = false;
  1759. rcu_read_lock();
  1760. idev = __in6_dev_get(dev);
  1761. if (idev) {
  1762. list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
  1763. ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
  1764. if (ret)
  1765. break;
  1766. }
  1767. }
  1768. rcu_read_unlock();
  1769. return ret;
  1770. }
  1771. EXPORT_SYMBOL(ipv6_chk_custom_prefix);
  1772. int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
  1773. {
  1774. const struct inet6_ifaddr *ifa;
  1775. const struct inet6_dev *idev;
  1776. int onlink;
  1777. onlink = 0;
  1778. rcu_read_lock();
  1779. idev = __in6_dev_get(dev);
  1780. if (idev) {
  1781. list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
  1782. onlink = ipv6_prefix_equal(addr, &ifa->addr,
  1783. ifa->prefix_len);
  1784. if (onlink)
  1785. break;
  1786. }
  1787. }
  1788. rcu_read_unlock();
  1789. return onlink;
  1790. }
  1791. EXPORT_SYMBOL(ipv6_chk_prefix);
  1792. /**
  1793. * ipv6_dev_find - find the first device with a given source address.
  1794. * @net: the net namespace
  1795. * @addr: the source address
  1796. * @dev: used to find the L3 domain of interest
  1797. *
  1798. * The caller should be protected by RCU, or RTNL.
  1799. */
  1800. struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
  1801. struct net_device *dev)
  1802. {
  1803. return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
  1804. IFA_F_TENTATIVE);
  1805. }
  1806. EXPORT_SYMBOL(ipv6_dev_find);
  1807. struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
  1808. struct net_device *dev, int strict)
  1809. {
  1810. unsigned int hash = inet6_addr_hash(net, addr);
  1811. struct inet6_ifaddr *ifp, *result = NULL;
  1812. rcu_read_lock();
  1813. hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
  1814. if (ipv6_addr_equal(&ifp->addr, addr)) {
  1815. if (!dev || ifp->idev->dev == dev ||
  1816. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
  1817. if (in6_ifa_hold_safe(ifp)) {
  1818. result = ifp;
  1819. break;
  1820. }
  1821. }
  1822. }
  1823. }
  1824. rcu_read_unlock();
  1825. return result;
  1826. }
  1827. /* Gets referenced address, destroys ifaddr */
  1828. static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
  1829. {
  1830. if (dad_failed)
  1831. ifp->flags |= IFA_F_DADFAILED;
  1832. if (ifp->flags&IFA_F_TEMPORARY) {
  1833. struct inet6_ifaddr *ifpub;
  1834. spin_lock_bh(&ifp->lock);
  1835. ifpub = ifp->ifpub;
  1836. if (ifpub) {
  1837. in6_ifa_hold(ifpub);
  1838. spin_unlock_bh(&ifp->lock);
  1839. ipv6_create_tempaddr(ifpub, true);
  1840. in6_ifa_put(ifpub);
  1841. } else {
  1842. spin_unlock_bh(&ifp->lock);
  1843. }
  1844. ipv6_del_addr(ifp);
  1845. } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
  1846. spin_lock_bh(&ifp->lock);
  1847. addrconf_del_dad_work(ifp);
  1848. ifp->flags |= IFA_F_TENTATIVE;
  1849. if (dad_failed)
  1850. ifp->flags &= ~IFA_F_OPTIMISTIC;
  1851. spin_unlock_bh(&ifp->lock);
  1852. if (dad_failed)
  1853. ipv6_ifa_notify(0, ifp);
  1854. in6_ifa_put(ifp);
  1855. } else {
  1856. ipv6_del_addr(ifp);
  1857. }
  1858. }
  1859. static int addrconf_dad_end(struct inet6_ifaddr *ifp)
  1860. {
  1861. int err = -ENOENT;
  1862. spin_lock_bh(&ifp->lock);
  1863. if (ifp->state == INET6_IFADDR_STATE_DAD) {
  1864. ifp->state = INET6_IFADDR_STATE_POSTDAD;
  1865. err = 0;
  1866. }
  1867. spin_unlock_bh(&ifp->lock);
  1868. return err;
  1869. }
  1870. void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
  1871. {
  1872. struct inet6_dev *idev = ifp->idev;
  1873. struct net *net = dev_net(idev->dev);
  1874. int max_addresses;
  1875. if (addrconf_dad_end(ifp)) {
  1876. in6_ifa_put(ifp);
  1877. return;
  1878. }
  1879. net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
  1880. ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
  1881. spin_lock_bh(&ifp->lock);
  1882. if (ifp->flags & IFA_F_STABLE_PRIVACY) {
  1883. struct in6_addr new_addr;
  1884. struct inet6_ifaddr *ifp2;
  1885. int retries = ifp->stable_privacy_retry + 1;
  1886. struct ifa6_config cfg = {
  1887. .pfx = &new_addr,
  1888. .plen = ifp->prefix_len,
  1889. .ifa_flags = ifp->flags,
  1890. .valid_lft = ifp->valid_lft,
  1891. .preferred_lft = ifp->prefered_lft,
  1892. .scope = ifp->scope,
  1893. };
  1894. if (retries > net->ipv6.sysctl.idgen_retries) {
  1895. net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
  1896. ifp->idev->dev->name);
  1897. goto errdad;
  1898. }
  1899. new_addr = ifp->addr;
  1900. if (ipv6_generate_stable_address(&new_addr, retries,
  1901. idev))
  1902. goto errdad;
  1903. spin_unlock_bh(&ifp->lock);
  1904. max_addresses = READ_ONCE(idev->cnf.max_addresses);
  1905. if (max_addresses &&
  1906. ipv6_count_addresses(idev) >= max_addresses)
  1907. goto lock_errdad;
  1908. net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
  1909. ifp->idev->dev->name);
  1910. ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
  1911. if (IS_ERR(ifp2))
  1912. goto lock_errdad;
  1913. spin_lock_bh(&ifp2->lock);
  1914. ifp2->stable_privacy_retry = retries;
  1915. ifp2->state = INET6_IFADDR_STATE_PREDAD;
  1916. spin_unlock_bh(&ifp2->lock);
  1917. addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
  1918. in6_ifa_put(ifp2);
  1919. lock_errdad:
  1920. spin_lock_bh(&ifp->lock);
  1921. }
  1922. errdad:
  1923. /* transition from _POSTDAD to _ERRDAD */
  1924. ifp->state = INET6_IFADDR_STATE_ERRDAD;
  1925. spin_unlock_bh(&ifp->lock);
  1926. addrconf_mod_dad_work(ifp, 0);
  1927. in6_ifa_put(ifp);
  1928. }
  1929. /* Join to solicited addr multicast group. */
  1930. void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
  1931. {
  1932. struct in6_addr maddr;
  1933. if (READ_ONCE(dev->flags) & (IFF_LOOPBACK | IFF_NOARP))
  1934. return;
  1935. addrconf_addr_solict_mult(addr, &maddr);
  1936. ipv6_dev_mc_inc(dev, &maddr);
  1937. }
  1938. /* caller must hold RTNL */
  1939. void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
  1940. {
  1941. struct in6_addr maddr;
  1942. if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1943. return;
  1944. addrconf_addr_solict_mult(addr, &maddr);
  1945. __ipv6_dev_mc_dec(idev, &maddr);
  1946. }
  1947. /* caller must hold RTNL */
  1948. static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
  1949. {
  1950. struct in6_addr addr;
  1951. if (ifp->prefix_len >= 127) /* RFC 6164 */
  1952. return;
  1953. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1954. if (ipv6_addr_any(&addr))
  1955. return;
  1956. __ipv6_dev_ac_inc(ifp->idev, &addr);
  1957. }
  1958. /* caller must hold RTNL */
  1959. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
  1960. {
  1961. struct in6_addr addr;
  1962. if (ifp->prefix_len >= 127) /* RFC 6164 */
  1963. return;
  1964. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1965. if (ipv6_addr_any(&addr))
  1966. return;
  1967. __ipv6_dev_ac_dec(ifp->idev, &addr);
  1968. }
  1969. static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
  1970. {
  1971. switch (dev->addr_len) {
  1972. case ETH_ALEN:
  1973. memcpy(eui, dev->dev_addr, 3);
  1974. eui[3] = 0xFF;
  1975. eui[4] = 0xFE;
  1976. memcpy(eui + 5, dev->dev_addr + 3, 3);
  1977. break;
  1978. case EUI64_ADDR_LEN:
  1979. memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
  1980. eui[0] ^= 2;
  1981. break;
  1982. default:
  1983. return -1;
  1984. }
  1985. return 0;
  1986. }
  1987. static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
  1988. {
  1989. const union fwnet_hwaddr *ha;
  1990. if (dev->addr_len != FWNET_ALEN)
  1991. return -1;
  1992. ha = (const union fwnet_hwaddr *)dev->dev_addr;
  1993. memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
  1994. eui[0] ^= 2;
  1995. return 0;
  1996. }
  1997. static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
  1998. {
  1999. /* XXX: inherit EUI-64 from other interface -- yoshfuji */
  2000. if (dev->addr_len != ARCNET_ALEN)
  2001. return -1;
  2002. memset(eui, 0, 7);
  2003. eui[7] = *(u8 *)dev->dev_addr;
  2004. return 0;
  2005. }
  2006. static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
  2007. {
  2008. if (dev->addr_len != INFINIBAND_ALEN)
  2009. return -1;
  2010. memcpy(eui, dev->dev_addr + 12, 8);
  2011. eui[0] |= 2;
  2012. return 0;
  2013. }
  2014. static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
  2015. {
  2016. if (addr == 0)
  2017. return -1;
  2018. eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
  2019. ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
  2020. ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
  2021. ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
  2022. ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
  2023. ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
  2024. eui[1] = 0;
  2025. eui[2] = 0x5E;
  2026. eui[3] = 0xFE;
  2027. memcpy(eui + 4, &addr, 4);
  2028. return 0;
  2029. }
  2030. static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
  2031. {
  2032. if (dev->priv_flags & IFF_ISATAP)
  2033. return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
  2034. return -1;
  2035. }
  2036. static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
  2037. {
  2038. return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
  2039. }
  2040. static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
  2041. {
  2042. memcpy(eui, dev->perm_addr, 3);
  2043. memcpy(eui + 5, dev->perm_addr + 3, 3);
  2044. eui[3] = 0xFF;
  2045. eui[4] = 0xFE;
  2046. eui[0] ^= 2;
  2047. return 0;
  2048. }
  2049. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
  2050. {
  2051. switch (dev->type) {
  2052. case ARPHRD_ETHER:
  2053. case ARPHRD_FDDI:
  2054. return addrconf_ifid_eui48(eui, dev);
  2055. case ARPHRD_ARCNET:
  2056. return addrconf_ifid_arcnet(eui, dev);
  2057. case ARPHRD_INFINIBAND:
  2058. return addrconf_ifid_infiniband(eui, dev);
  2059. case ARPHRD_SIT:
  2060. return addrconf_ifid_sit(eui, dev);
  2061. case ARPHRD_IPGRE:
  2062. case ARPHRD_TUNNEL:
  2063. return addrconf_ifid_gre(eui, dev);
  2064. case ARPHRD_6LOWPAN:
  2065. return addrconf_ifid_6lowpan(eui, dev);
  2066. case ARPHRD_IEEE1394:
  2067. return addrconf_ifid_ieee1394(eui, dev);
  2068. case ARPHRD_TUNNEL6:
  2069. case ARPHRD_IP6GRE:
  2070. case ARPHRD_RAWIP:
  2071. return addrconf_ifid_ip6tnl(eui, dev);
  2072. }
  2073. return -1;
  2074. }
  2075. static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
  2076. {
  2077. int err = -1;
  2078. struct inet6_ifaddr *ifp;
  2079. read_lock_bh(&idev->lock);
  2080. list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
  2081. if (ifp->scope > IFA_LINK)
  2082. break;
  2083. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  2084. memcpy(eui, ifp->addr.s6_addr+8, 8);
  2085. err = 0;
  2086. break;
  2087. }
  2088. }
  2089. read_unlock_bh(&idev->lock);
  2090. return err;
  2091. }
  2092. /* Generation of a randomized Interface Identifier
  2093. * draft-ietf-6man-rfc4941bis, Section 3.3.1
  2094. */
  2095. static void ipv6_gen_rnd_iid(struct in6_addr *addr)
  2096. {
  2097. regen:
  2098. get_random_bytes(&addr->s6_addr[8], 8);
  2099. /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
  2100. * check if generated address is not inappropriate:
  2101. *
  2102. * - Reserved IPv6 Interface Identifiers
  2103. * - XXX: already assigned to an address on the device
  2104. */
  2105. /* Subnet-router anycast: 0000:0000:0000:0000 */
  2106. if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
  2107. goto regen;
  2108. /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
  2109. * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
  2110. * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
  2111. */
  2112. if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
  2113. (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
  2114. goto regen;
  2115. /* Reserved subnet anycast addresses */
  2116. if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
  2117. ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
  2118. goto regen;
  2119. }
  2120. /*
  2121. * Add prefix route.
  2122. */
  2123. static void
  2124. addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
  2125. struct net_device *dev, unsigned long expires,
  2126. u32 flags, gfp_t gfp_flags)
  2127. {
  2128. struct fib6_config cfg = {
  2129. .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
  2130. .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
  2131. .fc_ifindex = dev->ifindex,
  2132. .fc_expires = expires,
  2133. .fc_dst_len = plen,
  2134. .fc_flags = RTF_UP | flags,
  2135. .fc_nlinfo.nl_net = dev_net(dev),
  2136. .fc_protocol = RTPROT_KERNEL,
  2137. .fc_type = RTN_UNICAST,
  2138. };
  2139. cfg.fc_dst = *pfx;
  2140. /* Prevent useless cloning on PtP SIT.
  2141. This thing is done here expecting that the whole
  2142. class of non-broadcast devices need not cloning.
  2143. */
  2144. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2145. if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
  2146. cfg.fc_flags |= RTF_NONEXTHOP;
  2147. #endif
  2148. ip6_route_add(&cfg, gfp_flags, NULL);
  2149. }
  2150. static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
  2151. int plen,
  2152. const struct net_device *dev,
  2153. u32 flags, u32 noflags,
  2154. bool no_gw)
  2155. {
  2156. struct fib6_node *fn;
  2157. struct fib6_info *rt = NULL;
  2158. struct fib6_table *table;
  2159. u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
  2160. table = fib6_get_table(dev_net(dev), tb_id);
  2161. if (!table)
  2162. return NULL;
  2163. rcu_read_lock();
  2164. fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
  2165. if (!fn)
  2166. goto out;
  2167. for_each_fib6_node_rt_rcu(fn) {
  2168. /* prefix routes only use builtin fib6_nh */
  2169. if (rt->nh)
  2170. continue;
  2171. if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
  2172. continue;
  2173. if (no_gw && rt->fib6_nh->fib_nh_gw_family)
  2174. continue;
  2175. if ((rt->fib6_flags & flags) != flags)
  2176. continue;
  2177. if ((rt->fib6_flags & noflags) != 0)
  2178. continue;
  2179. if (!fib6_info_hold_safe(rt))
  2180. continue;
  2181. break;
  2182. }
  2183. out:
  2184. rcu_read_unlock();
  2185. return rt;
  2186. }
  2187. /* Create "default" multicast route to the interface */
  2188. static void addrconf_add_mroute(struct net_device *dev)
  2189. {
  2190. struct fib6_config cfg = {
  2191. .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
  2192. .fc_metric = IP6_RT_PRIO_ADDRCONF,
  2193. .fc_ifindex = dev->ifindex,
  2194. .fc_dst_len = 8,
  2195. .fc_flags = RTF_UP,
  2196. .fc_type = RTN_MULTICAST,
  2197. .fc_nlinfo.nl_net = dev_net(dev),
  2198. .fc_protocol = RTPROT_KERNEL,
  2199. };
  2200. ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
  2201. ip6_route_add(&cfg, GFP_KERNEL, NULL);
  2202. }
  2203. static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
  2204. {
  2205. struct inet6_dev *idev;
  2206. ASSERT_RTNL();
  2207. idev = ipv6_find_idev(dev);
  2208. if (IS_ERR(idev))
  2209. return idev;
  2210. if (idev->cnf.disable_ipv6)
  2211. return ERR_PTR(-EACCES);
  2212. /* Add default multicast route */
  2213. if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
  2214. addrconf_add_mroute(dev);
  2215. return idev;
  2216. }
  2217. static void delete_tempaddrs(struct inet6_dev *idev,
  2218. struct inet6_ifaddr *ifp)
  2219. {
  2220. struct inet6_ifaddr *ift, *tmp;
  2221. write_lock_bh(&idev->lock);
  2222. list_for_each_entry_safe(ift, tmp, &idev->tempaddr_list, tmp_list) {
  2223. if (ift->ifpub != ifp)
  2224. continue;
  2225. in6_ifa_hold(ift);
  2226. write_unlock_bh(&idev->lock);
  2227. ipv6_del_addr(ift);
  2228. write_lock_bh(&idev->lock);
  2229. }
  2230. write_unlock_bh(&idev->lock);
  2231. }
  2232. static void manage_tempaddrs(struct inet6_dev *idev,
  2233. struct inet6_ifaddr *ifp,
  2234. __u32 valid_lft, __u32 prefered_lft,
  2235. bool create, unsigned long now)
  2236. {
  2237. u32 flags;
  2238. struct inet6_ifaddr *ift;
  2239. read_lock_bh(&idev->lock);
  2240. /* update all temporary addresses in the list */
  2241. list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
  2242. int age, max_valid, max_prefered;
  2243. if (ifp != ift->ifpub)
  2244. continue;
  2245. /* RFC 4941 section 3.3:
  2246. * If a received option will extend the lifetime of a public
  2247. * address, the lifetimes of temporary addresses should
  2248. * be extended, subject to the overall constraint that no
  2249. * temporary addresses should ever remain "valid" or "preferred"
  2250. * for a time longer than (TEMP_VALID_LIFETIME) or
  2251. * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
  2252. */
  2253. age = (now - ift->cstamp) / HZ;
  2254. max_valid = READ_ONCE(idev->cnf.temp_valid_lft) - age;
  2255. if (max_valid < 0)
  2256. max_valid = 0;
  2257. max_prefered = READ_ONCE(idev->cnf.temp_prefered_lft) -
  2258. idev->desync_factor - age;
  2259. if (max_prefered < 0)
  2260. max_prefered = 0;
  2261. if (valid_lft > max_valid)
  2262. valid_lft = max_valid;
  2263. if (prefered_lft > max_prefered)
  2264. prefered_lft = max_prefered;
  2265. spin_lock(&ift->lock);
  2266. flags = ift->flags;
  2267. ift->valid_lft = valid_lft;
  2268. ift->prefered_lft = prefered_lft;
  2269. ift->tstamp = now;
  2270. if (prefered_lft > 0)
  2271. ift->flags &= ~IFA_F_DEPRECATED;
  2272. spin_unlock(&ift->lock);
  2273. if (!(flags&IFA_F_TENTATIVE))
  2274. ipv6_ifa_notify(0, ift);
  2275. }
  2276. /* Also create a temporary address if it's enabled but no temporary
  2277. * address currently exists.
  2278. * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
  2279. * as part of cleanup (ie. deleting the mngtmpaddr).
  2280. * We don't want that to result in creating a new temporary ip address.
  2281. */
  2282. if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
  2283. create = true;
  2284. if (create && READ_ONCE(idev->cnf.use_tempaddr) > 0) {
  2285. /* When a new public address is created as described
  2286. * in [ADDRCONF], also create a new temporary address.
  2287. */
  2288. read_unlock_bh(&idev->lock);
  2289. ipv6_create_tempaddr(ifp, false);
  2290. } else {
  2291. read_unlock_bh(&idev->lock);
  2292. }
  2293. }
  2294. static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
  2295. {
  2296. return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
  2297. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
  2298. }
  2299. int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
  2300. const struct prefix_info *pinfo,
  2301. struct inet6_dev *in6_dev,
  2302. const struct in6_addr *addr, int addr_type,
  2303. u32 addr_flags, bool sllao, bool tokenized,
  2304. __u32 valid_lft, u32 prefered_lft)
  2305. {
  2306. struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
  2307. int create = 0, update_lft = 0;
  2308. if (!ifp && valid_lft) {
  2309. int max_addresses = READ_ONCE(in6_dev->cnf.max_addresses);
  2310. struct ifa6_config cfg = {
  2311. .pfx = addr,
  2312. .plen = pinfo->prefix_len,
  2313. .ifa_flags = addr_flags,
  2314. .valid_lft = valid_lft,
  2315. .preferred_lft = prefered_lft,
  2316. .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
  2317. .ifa_proto = IFAPROT_KERNEL_RA
  2318. };
  2319. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2320. if ((READ_ONCE(net->ipv6.devconf_all->optimistic_dad) ||
  2321. READ_ONCE(in6_dev->cnf.optimistic_dad)) &&
  2322. !net->ipv6.devconf_all->forwarding && sllao)
  2323. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  2324. #endif
  2325. /* Do not allow to create too much of autoconfigured
  2326. * addresses; this would be too easy way to crash kernel.
  2327. */
  2328. if (!max_addresses ||
  2329. ipv6_count_addresses(in6_dev) < max_addresses)
  2330. ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
  2331. if (IS_ERR_OR_NULL(ifp))
  2332. return -1;
  2333. create = 1;
  2334. spin_lock_bh(&ifp->lock);
  2335. ifp->flags |= IFA_F_MANAGETEMPADDR;
  2336. ifp->cstamp = jiffies;
  2337. ifp->tokenized = tokenized;
  2338. spin_unlock_bh(&ifp->lock);
  2339. addrconf_dad_start(ifp);
  2340. }
  2341. if (ifp) {
  2342. u32 flags;
  2343. unsigned long now;
  2344. u32 stored_lft;
  2345. /* update lifetime (RFC2462 5.5.3 e) */
  2346. spin_lock_bh(&ifp->lock);
  2347. now = jiffies;
  2348. if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
  2349. stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
  2350. else
  2351. stored_lft = 0;
  2352. /* RFC4862 Section 5.5.3e:
  2353. * "Note that the preferred lifetime of the
  2354. * corresponding address is always reset to
  2355. * the Preferred Lifetime in the received
  2356. * Prefix Information option, regardless of
  2357. * whether the valid lifetime is also reset or
  2358. * ignored."
  2359. *
  2360. * So we should always update prefered_lft here.
  2361. */
  2362. update_lft = !create && stored_lft;
  2363. if (update_lft && !READ_ONCE(in6_dev->cnf.ra_honor_pio_life)) {
  2364. const u32 minimum_lft = min_t(u32,
  2365. stored_lft, MIN_VALID_LIFETIME);
  2366. valid_lft = max(valid_lft, minimum_lft);
  2367. }
  2368. if (update_lft) {
  2369. ifp->valid_lft = valid_lft;
  2370. ifp->prefered_lft = prefered_lft;
  2371. WRITE_ONCE(ifp->tstamp, now);
  2372. flags = ifp->flags;
  2373. ifp->flags &= ~IFA_F_DEPRECATED;
  2374. spin_unlock_bh(&ifp->lock);
  2375. if (!(flags&IFA_F_TENTATIVE))
  2376. ipv6_ifa_notify(0, ifp);
  2377. } else
  2378. spin_unlock_bh(&ifp->lock);
  2379. manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
  2380. create, now);
  2381. in6_ifa_put(ifp);
  2382. addrconf_verify(net);
  2383. }
  2384. return 0;
  2385. }
  2386. EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
  2387. void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
  2388. {
  2389. struct prefix_info *pinfo;
  2390. struct fib6_table *table;
  2391. __u32 valid_lft;
  2392. __u32 prefered_lft;
  2393. int addr_type, err;
  2394. u32 addr_flags = 0;
  2395. struct inet6_dev *in6_dev;
  2396. struct net *net = dev_net(dev);
  2397. bool ignore_autoconf = false;
  2398. pinfo = (struct prefix_info *) opt;
  2399. if (len < sizeof(struct prefix_info)) {
  2400. netdev_dbg(dev, "addrconf: prefix option too short\n");
  2401. return;
  2402. }
  2403. /*
  2404. * Validation checks ([ADDRCONF], page 19)
  2405. */
  2406. addr_type = ipv6_addr_type(&pinfo->prefix);
  2407. if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
  2408. return;
  2409. valid_lft = ntohl(pinfo->valid);
  2410. prefered_lft = ntohl(pinfo->prefered);
  2411. if (prefered_lft > valid_lft) {
  2412. net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
  2413. return;
  2414. }
  2415. in6_dev = in6_dev_get(dev);
  2416. if (!in6_dev) {
  2417. net_dbg_ratelimited("addrconf: device %s not configured\n",
  2418. dev->name);
  2419. return;
  2420. }
  2421. if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft)
  2422. goto put;
  2423. /*
  2424. * Two things going on here:
  2425. * 1) Add routes for on-link prefixes
  2426. * 2) Configure prefixes with the auto flag set
  2427. */
  2428. if (pinfo->onlink) {
  2429. struct fib6_info *rt;
  2430. unsigned long rt_expires;
  2431. /* Avoid arithmetic overflow. Really, we could
  2432. * save rt_expires in seconds, likely valid_lft,
  2433. * but it would require division in fib gc, that it
  2434. * not good.
  2435. */
  2436. if (HZ > USER_HZ)
  2437. rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
  2438. else
  2439. rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
  2440. if (addrconf_finite_timeout(rt_expires))
  2441. rt_expires *= HZ;
  2442. rt = addrconf_get_prefix_route(&pinfo->prefix,
  2443. pinfo->prefix_len,
  2444. dev,
  2445. RTF_ADDRCONF | RTF_PREFIX_RT,
  2446. RTF_DEFAULT, true);
  2447. if (rt) {
  2448. /* Autoconf prefix route */
  2449. if (valid_lft == 0) {
  2450. ip6_del_rt(net, rt, false);
  2451. rt = NULL;
  2452. } else {
  2453. table = rt->fib6_table;
  2454. spin_lock_bh(&table->tb6_lock);
  2455. if (addrconf_finite_timeout(rt_expires)) {
  2456. /* not infinity */
  2457. fib6_set_expires(rt, jiffies + rt_expires);
  2458. fib6_add_gc_list(rt);
  2459. } else {
  2460. fib6_clean_expires(rt);
  2461. fib6_remove_gc_list(rt);
  2462. }
  2463. spin_unlock_bh(&table->tb6_lock);
  2464. }
  2465. } else if (valid_lft) {
  2466. clock_t expires = 0;
  2467. int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
  2468. if (addrconf_finite_timeout(rt_expires)) {
  2469. /* not infinity */
  2470. flags |= RTF_EXPIRES;
  2471. expires = jiffies_to_clock_t(rt_expires);
  2472. }
  2473. addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
  2474. 0, dev, expires, flags,
  2475. GFP_ATOMIC);
  2476. }
  2477. fib6_info_release(rt);
  2478. }
  2479. /* Try to figure out our local address for this prefix */
  2480. ignore_autoconf = READ_ONCE(in6_dev->cnf.ra_honor_pio_pflag) && pinfo->preferpd;
  2481. if (pinfo->autoconf && in6_dev->cnf.autoconf && !ignore_autoconf) {
  2482. struct in6_addr addr;
  2483. bool tokenized = false, dev_addr_generated = false;
  2484. if (pinfo->prefix_len == 64) {
  2485. memcpy(&addr, &pinfo->prefix, 8);
  2486. if (!ipv6_addr_any(&in6_dev->token)) {
  2487. read_lock_bh(&in6_dev->lock);
  2488. memcpy(addr.s6_addr + 8,
  2489. in6_dev->token.s6_addr + 8, 8);
  2490. read_unlock_bh(&in6_dev->lock);
  2491. tokenized = true;
  2492. } else if (is_addr_mode_generate_stable(in6_dev) &&
  2493. !ipv6_generate_stable_address(&addr, 0,
  2494. in6_dev)) {
  2495. addr_flags |= IFA_F_STABLE_PRIVACY;
  2496. goto ok;
  2497. } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
  2498. ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
  2499. goto put;
  2500. } else {
  2501. dev_addr_generated = true;
  2502. }
  2503. goto ok;
  2504. }
  2505. net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
  2506. pinfo->prefix_len);
  2507. goto put;
  2508. ok:
  2509. err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
  2510. &addr, addr_type,
  2511. addr_flags, sllao,
  2512. tokenized, valid_lft,
  2513. prefered_lft);
  2514. if (err)
  2515. goto put;
  2516. /* Ignore error case here because previous prefix add addr was
  2517. * successful which will be notified.
  2518. */
  2519. ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
  2520. addr_type, addr_flags, sllao,
  2521. tokenized, valid_lft,
  2522. prefered_lft,
  2523. dev_addr_generated);
  2524. }
  2525. inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
  2526. put:
  2527. in6_dev_put(in6_dev);
  2528. }
  2529. static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
  2530. struct in6_ifreq *ireq)
  2531. {
  2532. struct ip_tunnel_parm_kern p = { };
  2533. int err;
  2534. if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
  2535. return -EADDRNOTAVAIL;
  2536. p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
  2537. p.iph.version = 4;
  2538. p.iph.ihl = 5;
  2539. p.iph.protocol = IPPROTO_IPV6;
  2540. p.iph.ttl = 64;
  2541. if (!dev->netdev_ops->ndo_tunnel_ctl)
  2542. return -EOPNOTSUPP;
  2543. err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
  2544. if (err)
  2545. return err;
  2546. dev = __dev_get_by_name(net, p.name);
  2547. if (!dev)
  2548. return -ENOBUFS;
  2549. return dev_open(dev, NULL);
  2550. }
  2551. /*
  2552. * Set destination address.
  2553. * Special case for SIT interfaces where we create a new "virtual"
  2554. * device.
  2555. */
  2556. int addrconf_set_dstaddr(struct net *net, void __user *arg)
  2557. {
  2558. struct net_device *dev;
  2559. struct in6_ifreq ireq;
  2560. int err = -ENODEV;
  2561. if (!IS_ENABLED(CONFIG_IPV6_SIT))
  2562. return -ENODEV;
  2563. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2564. return -EFAULT;
  2565. rtnl_lock();
  2566. dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
  2567. if (dev && dev->type == ARPHRD_SIT)
  2568. err = addrconf_set_sit_dstaddr(net, dev, &ireq);
  2569. rtnl_unlock();
  2570. return err;
  2571. }
  2572. static int ipv6_mc_config(struct sock *sk, bool join,
  2573. const struct in6_addr *addr, int ifindex)
  2574. {
  2575. int ret;
  2576. ASSERT_RTNL();
  2577. lock_sock(sk);
  2578. if (join)
  2579. ret = ipv6_sock_mc_join(sk, ifindex, addr);
  2580. else
  2581. ret = ipv6_sock_mc_drop(sk, ifindex, addr);
  2582. release_sock(sk);
  2583. return ret;
  2584. }
  2585. /*
  2586. * Manual configuration of address on an interface
  2587. */
  2588. static int inet6_addr_add(struct net *net, int ifindex,
  2589. struct ifa6_config *cfg,
  2590. struct netlink_ext_ack *extack)
  2591. {
  2592. struct inet6_ifaddr *ifp;
  2593. struct inet6_dev *idev;
  2594. struct net_device *dev;
  2595. unsigned long timeout;
  2596. clock_t expires;
  2597. u32 flags;
  2598. ASSERT_RTNL();
  2599. if (cfg->plen > 128) {
  2600. NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
  2601. return -EINVAL;
  2602. }
  2603. /* check the lifetime */
  2604. if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) {
  2605. NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
  2606. return -EINVAL;
  2607. }
  2608. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) {
  2609. NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
  2610. return -EINVAL;
  2611. }
  2612. dev = __dev_get_by_index(net, ifindex);
  2613. if (!dev)
  2614. return -ENODEV;
  2615. idev = addrconf_add_dev(dev);
  2616. if (IS_ERR(idev)) {
  2617. NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
  2618. return PTR_ERR(idev);
  2619. }
  2620. if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
  2621. int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
  2622. true, cfg->pfx, ifindex);
  2623. if (ret < 0) {
  2624. NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed");
  2625. return ret;
  2626. }
  2627. }
  2628. cfg->scope = ipv6_addr_scope(cfg->pfx);
  2629. timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
  2630. if (addrconf_finite_timeout(timeout)) {
  2631. expires = jiffies_to_clock_t(timeout * HZ);
  2632. cfg->valid_lft = timeout;
  2633. flags = RTF_EXPIRES;
  2634. } else {
  2635. expires = 0;
  2636. flags = 0;
  2637. cfg->ifa_flags |= IFA_F_PERMANENT;
  2638. }
  2639. timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
  2640. if (addrconf_finite_timeout(timeout)) {
  2641. if (timeout == 0)
  2642. cfg->ifa_flags |= IFA_F_DEPRECATED;
  2643. cfg->preferred_lft = timeout;
  2644. }
  2645. ifp = ipv6_add_addr(idev, cfg, true, extack);
  2646. if (!IS_ERR(ifp)) {
  2647. if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
  2648. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  2649. ifp->rt_priority, dev, expires,
  2650. flags, GFP_KERNEL);
  2651. }
  2652. /* Send a netlink notification if DAD is enabled and
  2653. * optimistic flag is not set
  2654. */
  2655. if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
  2656. ipv6_ifa_notify(0, ifp);
  2657. /*
  2658. * Note that section 3.1 of RFC 4429 indicates
  2659. * that the Optimistic flag should not be set for
  2660. * manually configured addresses
  2661. */
  2662. addrconf_dad_start(ifp);
  2663. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
  2664. manage_tempaddrs(idev, ifp, cfg->valid_lft,
  2665. cfg->preferred_lft, true, jiffies);
  2666. in6_ifa_put(ifp);
  2667. addrconf_verify_rtnl(net);
  2668. return 0;
  2669. } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
  2670. ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
  2671. cfg->pfx, ifindex);
  2672. }
  2673. return PTR_ERR(ifp);
  2674. }
  2675. static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
  2676. const struct in6_addr *pfx, unsigned int plen,
  2677. struct netlink_ext_ack *extack)
  2678. {
  2679. struct inet6_ifaddr *ifp;
  2680. struct inet6_dev *idev;
  2681. struct net_device *dev;
  2682. if (plen > 128) {
  2683. NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
  2684. return -EINVAL;
  2685. }
  2686. dev = __dev_get_by_index(net, ifindex);
  2687. if (!dev) {
  2688. NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
  2689. return -ENODEV;
  2690. }
  2691. idev = __in6_dev_get(dev);
  2692. if (!idev) {
  2693. NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
  2694. return -ENXIO;
  2695. }
  2696. read_lock_bh(&idev->lock);
  2697. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  2698. if (ifp->prefix_len == plen &&
  2699. ipv6_addr_equal(pfx, &ifp->addr)) {
  2700. in6_ifa_hold(ifp);
  2701. read_unlock_bh(&idev->lock);
  2702. ipv6_del_addr(ifp);
  2703. if (!(ifp->flags & IFA_F_TEMPORARY) &&
  2704. (ifp->flags & IFA_F_MANAGETEMPADDR))
  2705. delete_tempaddrs(idev, ifp);
  2706. addrconf_verify_rtnl(net);
  2707. if (ipv6_addr_is_multicast(pfx)) {
  2708. ipv6_mc_config(net->ipv6.mc_autojoin_sk,
  2709. false, pfx, dev->ifindex);
  2710. }
  2711. return 0;
  2712. }
  2713. }
  2714. read_unlock_bh(&idev->lock);
  2715. NL_SET_ERR_MSG_MOD(extack, "address not found");
  2716. return -EADDRNOTAVAIL;
  2717. }
  2718. int addrconf_add_ifaddr(struct net *net, void __user *arg)
  2719. {
  2720. struct ifa6_config cfg = {
  2721. .ifa_flags = IFA_F_PERMANENT,
  2722. .preferred_lft = INFINITY_LIFE_TIME,
  2723. .valid_lft = INFINITY_LIFE_TIME,
  2724. };
  2725. struct in6_ifreq ireq;
  2726. int err;
  2727. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2728. return -EPERM;
  2729. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2730. return -EFAULT;
  2731. cfg.pfx = &ireq.ifr6_addr;
  2732. cfg.plen = ireq.ifr6_prefixlen;
  2733. rtnl_lock();
  2734. err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
  2735. rtnl_unlock();
  2736. return err;
  2737. }
  2738. int addrconf_del_ifaddr(struct net *net, void __user *arg)
  2739. {
  2740. struct in6_ifreq ireq;
  2741. int err;
  2742. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2743. return -EPERM;
  2744. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2745. return -EFAULT;
  2746. rtnl_lock();
  2747. err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
  2748. ireq.ifr6_prefixlen, NULL);
  2749. rtnl_unlock();
  2750. return err;
  2751. }
  2752. static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
  2753. int plen, int scope, u8 proto)
  2754. {
  2755. struct inet6_ifaddr *ifp;
  2756. struct ifa6_config cfg = {
  2757. .pfx = addr,
  2758. .plen = plen,
  2759. .ifa_flags = IFA_F_PERMANENT,
  2760. .valid_lft = INFINITY_LIFE_TIME,
  2761. .preferred_lft = INFINITY_LIFE_TIME,
  2762. .scope = scope,
  2763. .ifa_proto = proto
  2764. };
  2765. ifp = ipv6_add_addr(idev, &cfg, true, NULL);
  2766. if (!IS_ERR(ifp)) {
  2767. spin_lock_bh(&ifp->lock);
  2768. ifp->flags &= ~IFA_F_TENTATIVE;
  2769. spin_unlock_bh(&ifp->lock);
  2770. rt_genid_bump_ipv6(dev_net(idev->dev));
  2771. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2772. in6_ifa_put(ifp);
  2773. }
  2774. }
  2775. #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
  2776. static void add_v4_addrs(struct inet6_dev *idev)
  2777. {
  2778. struct in6_addr addr;
  2779. struct net_device *dev;
  2780. struct net *net = dev_net(idev->dev);
  2781. int scope, plen;
  2782. u32 pflags = 0;
  2783. ASSERT_RTNL();
  2784. memset(&addr, 0, sizeof(struct in6_addr));
  2785. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  2786. if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
  2787. scope = IPV6_ADDR_COMPATv4;
  2788. plen = 96;
  2789. pflags |= RTF_NONEXTHOP;
  2790. } else {
  2791. if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
  2792. return;
  2793. addr.s6_addr32[0] = htonl(0xfe800000);
  2794. scope = IFA_LINK;
  2795. plen = 64;
  2796. }
  2797. if (addr.s6_addr32[3]) {
  2798. add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
  2799. addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
  2800. GFP_KERNEL);
  2801. return;
  2802. }
  2803. for_each_netdev(net, dev) {
  2804. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  2805. if (in_dev && (dev->flags & IFF_UP)) {
  2806. struct in_ifaddr *ifa;
  2807. int flag = scope;
  2808. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  2809. addr.s6_addr32[3] = ifa->ifa_local;
  2810. if (ifa->ifa_scope == RT_SCOPE_LINK)
  2811. continue;
  2812. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  2813. if (idev->dev->flags&IFF_POINTOPOINT)
  2814. continue;
  2815. flag |= IFA_HOST;
  2816. }
  2817. add_addr(idev, &addr, plen, flag,
  2818. IFAPROT_UNSPEC);
  2819. addrconf_prefix_route(&addr, plen, 0, idev->dev,
  2820. 0, pflags, GFP_KERNEL);
  2821. }
  2822. }
  2823. }
  2824. }
  2825. #endif
  2826. static void init_loopback(struct net_device *dev)
  2827. {
  2828. struct inet6_dev *idev;
  2829. /* ::1 */
  2830. ASSERT_RTNL();
  2831. idev = ipv6_find_idev(dev);
  2832. if (IS_ERR(idev)) {
  2833. pr_debug("%s: add_dev failed\n", __func__);
  2834. return;
  2835. }
  2836. add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
  2837. }
  2838. void addrconf_add_linklocal(struct inet6_dev *idev,
  2839. const struct in6_addr *addr, u32 flags)
  2840. {
  2841. struct ifa6_config cfg = {
  2842. .pfx = addr,
  2843. .plen = 64,
  2844. .ifa_flags = flags | IFA_F_PERMANENT,
  2845. .valid_lft = INFINITY_LIFE_TIME,
  2846. .preferred_lft = INFINITY_LIFE_TIME,
  2847. .scope = IFA_LINK,
  2848. .ifa_proto = IFAPROT_KERNEL_LL
  2849. };
  2850. struct inet6_ifaddr *ifp;
  2851. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2852. if ((READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad) ||
  2853. READ_ONCE(idev->cnf.optimistic_dad)) &&
  2854. !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
  2855. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  2856. #endif
  2857. ifp = ipv6_add_addr(idev, &cfg, true, NULL);
  2858. if (!IS_ERR(ifp)) {
  2859. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
  2860. 0, 0, GFP_ATOMIC);
  2861. addrconf_dad_start(ifp);
  2862. in6_ifa_put(ifp);
  2863. }
  2864. }
  2865. EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
  2866. static bool ipv6_reserved_interfaceid(struct in6_addr address)
  2867. {
  2868. if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
  2869. return true;
  2870. if (address.s6_addr32[2] == htonl(0x02005eff) &&
  2871. ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
  2872. return true;
  2873. if (address.s6_addr32[2] == htonl(0xfdffffff) &&
  2874. ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
  2875. return true;
  2876. return false;
  2877. }
  2878. static int ipv6_generate_stable_address(struct in6_addr *address,
  2879. u8 dad_count,
  2880. const struct inet6_dev *idev)
  2881. {
  2882. static DEFINE_SPINLOCK(lock);
  2883. static __u32 digest[SHA1_DIGEST_WORDS];
  2884. static __u32 workspace[SHA1_WORKSPACE_WORDS];
  2885. static union {
  2886. char __data[SHA1_BLOCK_SIZE];
  2887. struct {
  2888. struct in6_addr secret;
  2889. __be32 prefix[2];
  2890. unsigned char hwaddr[MAX_ADDR_LEN];
  2891. u8 dad_count;
  2892. } __packed;
  2893. } data;
  2894. struct in6_addr secret;
  2895. struct in6_addr temp;
  2896. struct net *net = dev_net(idev->dev);
  2897. BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
  2898. if (idev->cnf.stable_secret.initialized)
  2899. secret = idev->cnf.stable_secret.secret;
  2900. else if (net->ipv6.devconf_dflt->stable_secret.initialized)
  2901. secret = net->ipv6.devconf_dflt->stable_secret.secret;
  2902. else
  2903. return -1;
  2904. retry:
  2905. spin_lock_bh(&lock);
  2906. sha1_init(digest);
  2907. memset(&data, 0, sizeof(data));
  2908. memset(workspace, 0, sizeof(workspace));
  2909. memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
  2910. data.prefix[0] = address->s6_addr32[0];
  2911. data.prefix[1] = address->s6_addr32[1];
  2912. data.secret = secret;
  2913. data.dad_count = dad_count;
  2914. sha1_transform(digest, data.__data, workspace);
  2915. temp = *address;
  2916. temp.s6_addr32[2] = (__force __be32)digest[0];
  2917. temp.s6_addr32[3] = (__force __be32)digest[1];
  2918. spin_unlock_bh(&lock);
  2919. if (ipv6_reserved_interfaceid(temp)) {
  2920. dad_count++;
  2921. if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
  2922. return -1;
  2923. goto retry;
  2924. }
  2925. *address = temp;
  2926. return 0;
  2927. }
  2928. static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
  2929. {
  2930. struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
  2931. if (s->initialized)
  2932. return;
  2933. s = &idev->cnf.stable_secret;
  2934. get_random_bytes(&s->secret, sizeof(s->secret));
  2935. s->initialized = true;
  2936. }
  2937. static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
  2938. {
  2939. struct in6_addr addr;
  2940. /* no link local addresses on L3 master devices */
  2941. if (netif_is_l3_master(idev->dev))
  2942. return;
  2943. /* no link local addresses on devices flagged as slaves */
  2944. if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
  2945. return;
  2946. ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
  2947. switch (idev->cnf.addr_gen_mode) {
  2948. case IN6_ADDR_GEN_MODE_RANDOM:
  2949. ipv6_gen_mode_random_init(idev);
  2950. fallthrough;
  2951. case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
  2952. if (!ipv6_generate_stable_address(&addr, 0, idev))
  2953. addrconf_add_linklocal(idev, &addr,
  2954. IFA_F_STABLE_PRIVACY);
  2955. else if (prefix_route)
  2956. addrconf_prefix_route(&addr, 64, 0, idev->dev,
  2957. 0, 0, GFP_KERNEL);
  2958. break;
  2959. case IN6_ADDR_GEN_MODE_EUI64:
  2960. /* addrconf_add_linklocal also adds a prefix_route and we
  2961. * only need to care about prefix routes if ipv6_generate_eui64
  2962. * couldn't generate one.
  2963. */
  2964. if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
  2965. addrconf_add_linklocal(idev, &addr, 0);
  2966. else if (prefix_route)
  2967. addrconf_prefix_route(&addr, 64, 0, idev->dev,
  2968. 0, 0, GFP_KERNEL);
  2969. break;
  2970. case IN6_ADDR_GEN_MODE_NONE:
  2971. default:
  2972. /* will not add any link local address */
  2973. break;
  2974. }
  2975. }
  2976. static void addrconf_dev_config(struct net_device *dev)
  2977. {
  2978. struct inet6_dev *idev;
  2979. ASSERT_RTNL();
  2980. if ((dev->type != ARPHRD_ETHER) &&
  2981. (dev->type != ARPHRD_FDDI) &&
  2982. (dev->type != ARPHRD_ARCNET) &&
  2983. (dev->type != ARPHRD_INFINIBAND) &&
  2984. (dev->type != ARPHRD_IEEE1394) &&
  2985. (dev->type != ARPHRD_TUNNEL6) &&
  2986. (dev->type != ARPHRD_6LOWPAN) &&
  2987. (dev->type != ARPHRD_TUNNEL) &&
  2988. (dev->type != ARPHRD_NONE) &&
  2989. (dev->type != ARPHRD_RAWIP)) {
  2990. /* Alas, we support only Ethernet autoconfiguration. */
  2991. idev = __in6_dev_get(dev);
  2992. if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
  2993. dev->flags & IFF_MULTICAST)
  2994. ipv6_mc_up(idev);
  2995. return;
  2996. }
  2997. idev = addrconf_add_dev(dev);
  2998. if (IS_ERR(idev))
  2999. return;
  3000. /* this device type has no EUI support */
  3001. if (dev->type == ARPHRD_NONE &&
  3002. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
  3003. WRITE_ONCE(idev->cnf.addr_gen_mode,
  3004. IN6_ADDR_GEN_MODE_RANDOM);
  3005. addrconf_addr_gen(idev, false);
  3006. }
  3007. #if IS_ENABLED(CONFIG_IPV6_SIT)
  3008. static void addrconf_sit_config(struct net_device *dev)
  3009. {
  3010. struct inet6_dev *idev;
  3011. ASSERT_RTNL();
  3012. /*
  3013. * Configure the tunnel with one of our IPv4
  3014. * addresses... we should configure all of
  3015. * our v4 addrs in the tunnel
  3016. */
  3017. idev = ipv6_find_idev(dev);
  3018. if (IS_ERR(idev)) {
  3019. pr_debug("%s: add_dev failed\n", __func__);
  3020. return;
  3021. }
  3022. if (dev->priv_flags & IFF_ISATAP) {
  3023. addrconf_addr_gen(idev, false);
  3024. return;
  3025. }
  3026. add_v4_addrs(idev);
  3027. if (dev->flags&IFF_POINTOPOINT)
  3028. addrconf_add_mroute(dev);
  3029. }
  3030. #endif
  3031. #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
  3032. static void addrconf_gre_config(struct net_device *dev)
  3033. {
  3034. struct inet6_dev *idev;
  3035. ASSERT_RTNL();
  3036. idev = addrconf_add_dev(dev);
  3037. if (IS_ERR(idev))
  3038. return;
  3039. /* Generate the IPv6 link-local address using addrconf_addr_gen(),
  3040. * unless we have an IPv4 GRE device not bound to an IP address and
  3041. * which is in EUI64 mode (as __ipv6_isatap_ifid() would fail in this
  3042. * case). Such devices fall back to add_v4_addrs() instead.
  3043. */
  3044. if (!(dev->type == ARPHRD_IPGRE && *(__be32 *)dev->dev_addr == 0 &&
  3045. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)) {
  3046. addrconf_addr_gen(idev, true);
  3047. return;
  3048. }
  3049. add_v4_addrs(idev);
  3050. }
  3051. #endif
  3052. static void addrconf_init_auto_addrs(struct net_device *dev)
  3053. {
  3054. switch (dev->type) {
  3055. #if IS_ENABLED(CONFIG_IPV6_SIT)
  3056. case ARPHRD_SIT:
  3057. addrconf_sit_config(dev);
  3058. break;
  3059. #endif
  3060. #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
  3061. case ARPHRD_IP6GRE:
  3062. case ARPHRD_IPGRE:
  3063. addrconf_gre_config(dev);
  3064. break;
  3065. #endif
  3066. case ARPHRD_LOOPBACK:
  3067. init_loopback(dev);
  3068. break;
  3069. default:
  3070. addrconf_dev_config(dev);
  3071. break;
  3072. }
  3073. }
  3074. static int fixup_permanent_addr(struct net *net,
  3075. struct inet6_dev *idev,
  3076. struct inet6_ifaddr *ifp)
  3077. {
  3078. /* !fib6_node means the host route was removed from the
  3079. * FIB, for example, if 'lo' device is taken down. In that
  3080. * case regenerate the host route.
  3081. */
  3082. if (!ifp->rt || !ifp->rt->fib6_node) {
  3083. struct fib6_info *f6i, *prev;
  3084. f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
  3085. GFP_ATOMIC, NULL);
  3086. if (IS_ERR(f6i))
  3087. return PTR_ERR(f6i);
  3088. /* ifp->rt can be accessed outside of rtnl */
  3089. spin_lock(&ifp->lock);
  3090. prev = ifp->rt;
  3091. ifp->rt = f6i;
  3092. spin_unlock(&ifp->lock);
  3093. fib6_info_release(prev);
  3094. }
  3095. if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
  3096. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  3097. ifp->rt_priority, idev->dev, 0, 0,
  3098. GFP_ATOMIC);
  3099. }
  3100. if (ifp->state == INET6_IFADDR_STATE_PREDAD)
  3101. addrconf_dad_start(ifp);
  3102. return 0;
  3103. }
  3104. static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
  3105. {
  3106. struct inet6_ifaddr *ifp, *tmp;
  3107. struct inet6_dev *idev;
  3108. idev = __in6_dev_get(dev);
  3109. if (!idev)
  3110. return;
  3111. write_lock_bh(&idev->lock);
  3112. list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
  3113. if ((ifp->flags & IFA_F_PERMANENT) &&
  3114. fixup_permanent_addr(net, idev, ifp) < 0) {
  3115. write_unlock_bh(&idev->lock);
  3116. in6_ifa_hold(ifp);
  3117. ipv6_del_addr(ifp);
  3118. write_lock_bh(&idev->lock);
  3119. net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
  3120. idev->dev->name, &ifp->addr);
  3121. }
  3122. }
  3123. write_unlock_bh(&idev->lock);
  3124. }
  3125. static int addrconf_notify(struct notifier_block *this, unsigned long event,
  3126. void *ptr)
  3127. {
  3128. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3129. struct netdev_notifier_change_info *change_info;
  3130. struct netdev_notifier_changeupper_info *info;
  3131. struct inet6_dev *idev = __in6_dev_get(dev);
  3132. struct net *net = dev_net(dev);
  3133. int run_pending = 0;
  3134. int err;
  3135. switch (event) {
  3136. case NETDEV_REGISTER:
  3137. if (!idev && dev->mtu >= IPV6_MIN_MTU) {
  3138. idev = ipv6_add_dev(dev);
  3139. if (IS_ERR(idev))
  3140. return notifier_from_errno(PTR_ERR(idev));
  3141. }
  3142. break;
  3143. case NETDEV_CHANGEMTU:
  3144. /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
  3145. if (dev->mtu < IPV6_MIN_MTU) {
  3146. addrconf_ifdown(dev, dev != net->loopback_dev);
  3147. break;
  3148. }
  3149. if (idev) {
  3150. rt6_mtu_change(dev, dev->mtu);
  3151. WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
  3152. break;
  3153. }
  3154. /* allocate new idev */
  3155. idev = ipv6_add_dev(dev);
  3156. if (IS_ERR(idev))
  3157. break;
  3158. /* device is still not ready */
  3159. if (!(idev->if_flags & IF_READY))
  3160. break;
  3161. run_pending = 1;
  3162. fallthrough;
  3163. case NETDEV_UP:
  3164. case NETDEV_CHANGE:
  3165. if (idev && idev->cnf.disable_ipv6)
  3166. break;
  3167. if (dev->priv_flags & IFF_NO_ADDRCONF) {
  3168. if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
  3169. dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
  3170. ipv6_mc_up(idev);
  3171. break;
  3172. }
  3173. if (event == NETDEV_UP) {
  3174. /* restore routes for permanent addresses */
  3175. addrconf_permanent_addr(net, dev);
  3176. if (!addrconf_link_ready(dev)) {
  3177. /* device is not ready yet. */
  3178. pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
  3179. dev->name);
  3180. break;
  3181. }
  3182. if (!idev && dev->mtu >= IPV6_MIN_MTU)
  3183. idev = ipv6_add_dev(dev);
  3184. if (!IS_ERR_OR_NULL(idev)) {
  3185. idev->if_flags |= IF_READY;
  3186. run_pending = 1;
  3187. }
  3188. } else if (event == NETDEV_CHANGE) {
  3189. if (!addrconf_link_ready(dev)) {
  3190. /* device is still not ready. */
  3191. rt6_sync_down_dev(dev, event);
  3192. break;
  3193. }
  3194. if (!IS_ERR_OR_NULL(idev)) {
  3195. if (idev->if_flags & IF_READY) {
  3196. /* device is already configured -
  3197. * but resend MLD reports, we might
  3198. * have roamed and need to update
  3199. * multicast snooping switches
  3200. */
  3201. ipv6_mc_up(idev);
  3202. change_info = ptr;
  3203. if (change_info->flags_changed & IFF_NOARP)
  3204. addrconf_dad_run(idev, true);
  3205. rt6_sync_up(dev, RTNH_F_LINKDOWN);
  3206. break;
  3207. }
  3208. idev->if_flags |= IF_READY;
  3209. }
  3210. pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
  3211. dev->name);
  3212. run_pending = 1;
  3213. }
  3214. addrconf_init_auto_addrs(dev);
  3215. if (!IS_ERR_OR_NULL(idev)) {
  3216. if (run_pending)
  3217. addrconf_dad_run(idev, false);
  3218. /* Device has an address by now */
  3219. rt6_sync_up(dev, RTNH_F_DEAD);
  3220. /*
  3221. * If the MTU changed during the interface down,
  3222. * when the interface up, the changed MTU must be
  3223. * reflected in the idev as well as routers.
  3224. */
  3225. if (idev->cnf.mtu6 != dev->mtu &&
  3226. dev->mtu >= IPV6_MIN_MTU) {
  3227. rt6_mtu_change(dev, dev->mtu);
  3228. WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
  3229. }
  3230. WRITE_ONCE(idev->tstamp, jiffies);
  3231. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  3232. /*
  3233. * If the changed mtu during down is lower than
  3234. * IPV6_MIN_MTU stop IPv6 on this interface.
  3235. */
  3236. if (dev->mtu < IPV6_MIN_MTU)
  3237. addrconf_ifdown(dev, dev != net->loopback_dev);
  3238. }
  3239. break;
  3240. case NETDEV_DOWN:
  3241. case NETDEV_UNREGISTER:
  3242. /*
  3243. * Remove all addresses from this interface.
  3244. */
  3245. addrconf_ifdown(dev, event != NETDEV_DOWN);
  3246. break;
  3247. case NETDEV_CHANGENAME:
  3248. if (idev) {
  3249. snmp6_unregister_dev(idev);
  3250. addrconf_sysctl_unregister(idev);
  3251. err = addrconf_sysctl_register(idev);
  3252. if (err)
  3253. return notifier_from_errno(err);
  3254. err = snmp6_register_dev(idev);
  3255. if (err) {
  3256. addrconf_sysctl_unregister(idev);
  3257. return notifier_from_errno(err);
  3258. }
  3259. }
  3260. break;
  3261. case NETDEV_PRE_TYPE_CHANGE:
  3262. case NETDEV_POST_TYPE_CHANGE:
  3263. if (idev)
  3264. addrconf_type_change(dev, event);
  3265. break;
  3266. case NETDEV_CHANGEUPPER:
  3267. info = ptr;
  3268. /* flush all routes if dev is linked to or unlinked from
  3269. * an L3 master device (e.g., VRF)
  3270. */
  3271. if (info->upper_dev && netif_is_l3_master(info->upper_dev))
  3272. addrconf_ifdown(dev, false);
  3273. }
  3274. return NOTIFY_OK;
  3275. }
  3276. /*
  3277. * addrconf module should be notified of a device going up
  3278. */
  3279. static struct notifier_block ipv6_dev_notf = {
  3280. .notifier_call = addrconf_notify,
  3281. .priority = ADDRCONF_NOTIFY_PRIORITY,
  3282. };
  3283. static void addrconf_type_change(struct net_device *dev, unsigned long event)
  3284. {
  3285. struct inet6_dev *idev;
  3286. ASSERT_RTNL();
  3287. idev = __in6_dev_get(dev);
  3288. if (event == NETDEV_POST_TYPE_CHANGE)
  3289. ipv6_mc_remap(idev);
  3290. else if (event == NETDEV_PRE_TYPE_CHANGE)
  3291. ipv6_mc_unmap(idev);
  3292. }
  3293. static bool addr_is_local(const struct in6_addr *addr)
  3294. {
  3295. return ipv6_addr_type(addr) &
  3296. (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
  3297. }
  3298. static int addrconf_ifdown(struct net_device *dev, bool unregister)
  3299. {
  3300. unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
  3301. struct net *net = dev_net(dev);
  3302. struct inet6_dev *idev;
  3303. struct inet6_ifaddr *ifa;
  3304. LIST_HEAD(tmp_addr_list);
  3305. bool keep_addr = false;
  3306. bool was_ready;
  3307. int state, i;
  3308. ASSERT_RTNL();
  3309. rt6_disable_ip(dev, event);
  3310. idev = __in6_dev_get(dev);
  3311. if (!idev)
  3312. return -ENODEV;
  3313. /*
  3314. * Step 1: remove reference to ipv6 device from parent device.
  3315. * Do not dev_put!
  3316. */
  3317. if (unregister) {
  3318. WRITE_ONCE(idev->dead, 1);
  3319. /* protected by rtnl_lock */
  3320. RCU_INIT_POINTER(dev->ip6_ptr, NULL);
  3321. /* Step 1.5: remove snmp6 entry */
  3322. snmp6_unregister_dev(idev);
  3323. }
  3324. /* combine the user config with event to determine if permanent
  3325. * addresses are to be removed from address hash table
  3326. */
  3327. if (!unregister && !idev->cnf.disable_ipv6) {
  3328. /* aggregate the system setting and interface setting */
  3329. int _keep_addr = READ_ONCE(net->ipv6.devconf_all->keep_addr_on_down);
  3330. if (!_keep_addr)
  3331. _keep_addr = READ_ONCE(idev->cnf.keep_addr_on_down);
  3332. keep_addr = (_keep_addr > 0);
  3333. }
  3334. /* Step 2: clear hash table */
  3335. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  3336. struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
  3337. spin_lock_bh(&net->ipv6.addrconf_hash_lock);
  3338. restart:
  3339. hlist_for_each_entry_rcu(ifa, h, addr_lst) {
  3340. if (ifa->idev == idev) {
  3341. addrconf_del_dad_work(ifa);
  3342. /* combined flag + permanent flag decide if
  3343. * address is retained on a down event
  3344. */
  3345. if (!keep_addr ||
  3346. !(ifa->flags & IFA_F_PERMANENT) ||
  3347. addr_is_local(&ifa->addr)) {
  3348. hlist_del_init_rcu(&ifa->addr_lst);
  3349. goto restart;
  3350. }
  3351. }
  3352. }
  3353. spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
  3354. }
  3355. write_lock_bh(&idev->lock);
  3356. addrconf_del_rs_timer(idev);
  3357. /* Step 2: clear flags for stateless addrconf, repeated down
  3358. * detection
  3359. */
  3360. was_ready = idev->if_flags & IF_READY;
  3361. if (!unregister)
  3362. idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
  3363. /* Step 3: clear tempaddr list */
  3364. while (!list_empty(&idev->tempaddr_list)) {
  3365. ifa = list_first_entry(&idev->tempaddr_list,
  3366. struct inet6_ifaddr, tmp_list);
  3367. list_del(&ifa->tmp_list);
  3368. write_unlock_bh(&idev->lock);
  3369. spin_lock_bh(&ifa->lock);
  3370. if (ifa->ifpub) {
  3371. in6_ifa_put(ifa->ifpub);
  3372. ifa->ifpub = NULL;
  3373. }
  3374. spin_unlock_bh(&ifa->lock);
  3375. in6_ifa_put(ifa);
  3376. write_lock_bh(&idev->lock);
  3377. }
  3378. list_for_each_entry(ifa, &idev->addr_list, if_list)
  3379. list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
  3380. write_unlock_bh(&idev->lock);
  3381. while (!list_empty(&tmp_addr_list)) {
  3382. struct fib6_info *rt = NULL;
  3383. bool keep;
  3384. ifa = list_first_entry(&tmp_addr_list,
  3385. struct inet6_ifaddr, if_list_aux);
  3386. list_del(&ifa->if_list_aux);
  3387. addrconf_del_dad_work(ifa);
  3388. keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
  3389. !addr_is_local(&ifa->addr);
  3390. spin_lock_bh(&ifa->lock);
  3391. if (keep) {
  3392. /* set state to skip the notifier below */
  3393. state = INET6_IFADDR_STATE_DEAD;
  3394. ifa->state = INET6_IFADDR_STATE_PREDAD;
  3395. if (!(ifa->flags & IFA_F_NODAD))
  3396. ifa->flags |= IFA_F_TENTATIVE;
  3397. rt = ifa->rt;
  3398. ifa->rt = NULL;
  3399. } else {
  3400. state = ifa->state;
  3401. ifa->state = INET6_IFADDR_STATE_DEAD;
  3402. }
  3403. spin_unlock_bh(&ifa->lock);
  3404. if (rt)
  3405. ip6_del_rt(net, rt, false);
  3406. if (state != INET6_IFADDR_STATE_DEAD) {
  3407. __ipv6_ifa_notify(RTM_DELADDR, ifa);
  3408. inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
  3409. } else {
  3410. if (idev->cnf.forwarding)
  3411. addrconf_leave_anycast(ifa);
  3412. addrconf_leave_solict(ifa->idev, &ifa->addr);
  3413. }
  3414. if (!keep) {
  3415. write_lock_bh(&idev->lock);
  3416. list_del_rcu(&ifa->if_list);
  3417. write_unlock_bh(&idev->lock);
  3418. in6_ifa_put(ifa);
  3419. }
  3420. }
  3421. /* Step 5: Discard anycast and multicast list */
  3422. if (unregister) {
  3423. ipv6_ac_destroy_dev(idev);
  3424. ipv6_mc_destroy_dev(idev);
  3425. } else if (was_ready) {
  3426. ipv6_mc_down(idev);
  3427. }
  3428. WRITE_ONCE(idev->tstamp, jiffies);
  3429. idev->ra_mtu = 0;
  3430. /* Last: Shot the device (if unregistered) */
  3431. if (unregister) {
  3432. addrconf_sysctl_unregister(idev);
  3433. neigh_parms_release(&nd_tbl, idev->nd_parms);
  3434. neigh_ifdown(&nd_tbl, dev);
  3435. in6_dev_put(idev);
  3436. }
  3437. return 0;
  3438. }
  3439. static void addrconf_rs_timer(struct timer_list *t)
  3440. {
  3441. struct inet6_dev *idev = from_timer(idev, t, rs_timer);
  3442. struct net_device *dev = idev->dev;
  3443. struct in6_addr lladdr;
  3444. int rtr_solicits;
  3445. write_lock(&idev->lock);
  3446. if (idev->dead || !(idev->if_flags & IF_READY))
  3447. goto out;
  3448. if (!ipv6_accept_ra(idev))
  3449. goto out;
  3450. /* Announcement received after solicitation was sent */
  3451. if (idev->if_flags & IF_RA_RCVD)
  3452. goto out;
  3453. rtr_solicits = READ_ONCE(idev->cnf.rtr_solicits);
  3454. if (idev->rs_probes++ < rtr_solicits || rtr_solicits < 0) {
  3455. write_unlock(&idev->lock);
  3456. if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  3457. ndisc_send_rs(dev, &lladdr,
  3458. &in6addr_linklocal_allrouters);
  3459. else
  3460. goto put;
  3461. write_lock(&idev->lock);
  3462. idev->rs_interval = rfc3315_s14_backoff_update(
  3463. idev->rs_interval,
  3464. READ_ONCE(idev->cnf.rtr_solicit_max_interval));
  3465. /* The wait after the last probe can be shorter */
  3466. addrconf_mod_rs_timer(idev, (idev->rs_probes ==
  3467. READ_ONCE(idev->cnf.rtr_solicits)) ?
  3468. READ_ONCE(idev->cnf.rtr_solicit_delay) :
  3469. idev->rs_interval);
  3470. } else {
  3471. /*
  3472. * Note: we do not support deprecated "all on-link"
  3473. * assumption any longer.
  3474. */
  3475. pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
  3476. }
  3477. out:
  3478. write_unlock(&idev->lock);
  3479. put:
  3480. in6_dev_put(idev);
  3481. }
  3482. /*
  3483. * Duplicate Address Detection
  3484. */
  3485. static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
  3486. {
  3487. struct inet6_dev *idev = ifp->idev;
  3488. unsigned long rand_num;
  3489. u64 nonce;
  3490. if (ifp->flags & IFA_F_OPTIMISTIC)
  3491. rand_num = 0;
  3492. else
  3493. rand_num = get_random_u32_below(
  3494. READ_ONCE(idev->cnf.rtr_solicit_delay) ? : 1);
  3495. nonce = 0;
  3496. if (READ_ONCE(idev->cnf.enhanced_dad) ||
  3497. READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad)) {
  3498. do
  3499. get_random_bytes(&nonce, 6);
  3500. while (nonce == 0);
  3501. }
  3502. ifp->dad_nonce = nonce;
  3503. ifp->dad_probes = READ_ONCE(idev->cnf.dad_transmits);
  3504. addrconf_mod_dad_work(ifp, rand_num);
  3505. }
  3506. static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
  3507. {
  3508. struct inet6_dev *idev = ifp->idev;
  3509. struct net_device *dev = idev->dev;
  3510. bool bump_id, notify = false;
  3511. struct net *net;
  3512. addrconf_join_solict(dev, &ifp->addr);
  3513. read_lock_bh(&idev->lock);
  3514. spin_lock(&ifp->lock);
  3515. if (ifp->state == INET6_IFADDR_STATE_DEAD)
  3516. goto out;
  3517. net = dev_net(dev);
  3518. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  3519. (READ_ONCE(net->ipv6.devconf_all->accept_dad) < 1 &&
  3520. READ_ONCE(idev->cnf.accept_dad) < 1) ||
  3521. !(ifp->flags&IFA_F_TENTATIVE) ||
  3522. ifp->flags & IFA_F_NODAD) {
  3523. bool send_na = false;
  3524. if (ifp->flags & IFA_F_TENTATIVE &&
  3525. !(ifp->flags & IFA_F_OPTIMISTIC))
  3526. send_na = true;
  3527. bump_id = ifp->flags & IFA_F_TENTATIVE;
  3528. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  3529. spin_unlock(&ifp->lock);
  3530. read_unlock_bh(&idev->lock);
  3531. addrconf_dad_completed(ifp, bump_id, send_na);
  3532. return;
  3533. }
  3534. if (!(idev->if_flags & IF_READY)) {
  3535. spin_unlock(&ifp->lock);
  3536. read_unlock_bh(&idev->lock);
  3537. /*
  3538. * If the device is not ready:
  3539. * - keep it tentative if it is a permanent address.
  3540. * - otherwise, kill it.
  3541. */
  3542. in6_ifa_hold(ifp);
  3543. addrconf_dad_stop(ifp, 0);
  3544. return;
  3545. }
  3546. /*
  3547. * Optimistic nodes can start receiving
  3548. * Frames right away
  3549. */
  3550. if (ifp->flags & IFA_F_OPTIMISTIC) {
  3551. ip6_ins_rt(net, ifp->rt);
  3552. if (ipv6_use_optimistic_addr(net, idev)) {
  3553. /* Because optimistic nodes can use this address,
  3554. * notify listeners. If DAD fails, RTM_DELADDR is sent.
  3555. */
  3556. notify = true;
  3557. }
  3558. }
  3559. addrconf_dad_kick(ifp);
  3560. out:
  3561. spin_unlock(&ifp->lock);
  3562. read_unlock_bh(&idev->lock);
  3563. if (notify)
  3564. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  3565. }
  3566. static void addrconf_dad_start(struct inet6_ifaddr *ifp)
  3567. {
  3568. bool begin_dad = false;
  3569. spin_lock_bh(&ifp->lock);
  3570. if (ifp->state != INET6_IFADDR_STATE_DEAD) {
  3571. ifp->state = INET6_IFADDR_STATE_PREDAD;
  3572. begin_dad = true;
  3573. }
  3574. spin_unlock_bh(&ifp->lock);
  3575. if (begin_dad)
  3576. addrconf_mod_dad_work(ifp, 0);
  3577. }
  3578. static void addrconf_dad_work(struct work_struct *w)
  3579. {
  3580. struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
  3581. struct inet6_ifaddr,
  3582. dad_work);
  3583. struct inet6_dev *idev = ifp->idev;
  3584. bool bump_id, disable_ipv6 = false;
  3585. struct in6_addr mcaddr;
  3586. enum {
  3587. DAD_PROCESS,
  3588. DAD_BEGIN,
  3589. DAD_ABORT,
  3590. } action = DAD_PROCESS;
  3591. rtnl_lock();
  3592. spin_lock_bh(&ifp->lock);
  3593. if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
  3594. action = DAD_BEGIN;
  3595. ifp->state = INET6_IFADDR_STATE_DAD;
  3596. } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
  3597. action = DAD_ABORT;
  3598. ifp->state = INET6_IFADDR_STATE_POSTDAD;
  3599. if ((READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->accept_dad) > 1 ||
  3600. READ_ONCE(idev->cnf.accept_dad) > 1) &&
  3601. !idev->cnf.disable_ipv6 &&
  3602. !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
  3603. struct in6_addr addr;
  3604. addr.s6_addr32[0] = htonl(0xfe800000);
  3605. addr.s6_addr32[1] = 0;
  3606. if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
  3607. ipv6_addr_equal(&ifp->addr, &addr)) {
  3608. /* DAD failed for link-local based on MAC */
  3609. WRITE_ONCE(idev->cnf.disable_ipv6, 1);
  3610. pr_info("%s: IPv6 being disabled!\n",
  3611. ifp->idev->dev->name);
  3612. disable_ipv6 = true;
  3613. }
  3614. }
  3615. }
  3616. spin_unlock_bh(&ifp->lock);
  3617. if (action == DAD_BEGIN) {
  3618. addrconf_dad_begin(ifp);
  3619. goto out;
  3620. } else if (action == DAD_ABORT) {
  3621. in6_ifa_hold(ifp);
  3622. addrconf_dad_stop(ifp, 1);
  3623. if (disable_ipv6)
  3624. addrconf_ifdown(idev->dev, false);
  3625. goto out;
  3626. }
  3627. if (!ifp->dad_probes && addrconf_dad_end(ifp))
  3628. goto out;
  3629. write_lock_bh(&idev->lock);
  3630. if (idev->dead || !(idev->if_flags & IF_READY)) {
  3631. write_unlock_bh(&idev->lock);
  3632. goto out;
  3633. }
  3634. spin_lock(&ifp->lock);
  3635. if (ifp->state == INET6_IFADDR_STATE_DEAD) {
  3636. spin_unlock(&ifp->lock);
  3637. write_unlock_bh(&idev->lock);
  3638. goto out;
  3639. }
  3640. if (ifp->dad_probes == 0) {
  3641. bool send_na = false;
  3642. /*
  3643. * DAD was successful
  3644. */
  3645. if (ifp->flags & IFA_F_TENTATIVE &&
  3646. !(ifp->flags & IFA_F_OPTIMISTIC))
  3647. send_na = true;
  3648. bump_id = ifp->flags & IFA_F_TENTATIVE;
  3649. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  3650. spin_unlock(&ifp->lock);
  3651. write_unlock_bh(&idev->lock);
  3652. addrconf_dad_completed(ifp, bump_id, send_na);
  3653. goto out;
  3654. }
  3655. ifp->dad_probes--;
  3656. addrconf_mod_dad_work(ifp,
  3657. max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
  3658. HZ/100));
  3659. spin_unlock(&ifp->lock);
  3660. write_unlock_bh(&idev->lock);
  3661. /* send a neighbour solicitation for our addr */
  3662. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  3663. ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
  3664. ifp->dad_nonce);
  3665. out:
  3666. in6_ifa_put(ifp);
  3667. rtnl_unlock();
  3668. }
  3669. /* ifp->idev must be at least read locked */
  3670. static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
  3671. {
  3672. struct inet6_ifaddr *ifpiter;
  3673. struct inet6_dev *idev = ifp->idev;
  3674. list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
  3675. if (ifpiter->scope > IFA_LINK)
  3676. break;
  3677. if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
  3678. (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
  3679. IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
  3680. IFA_F_PERMANENT)
  3681. return false;
  3682. }
  3683. return true;
  3684. }
  3685. static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
  3686. bool send_na)
  3687. {
  3688. struct net_device *dev = ifp->idev->dev;
  3689. struct in6_addr lladdr;
  3690. bool send_rs, send_mld;
  3691. addrconf_del_dad_work(ifp);
  3692. /*
  3693. * Configure the address for reception. Now it is valid.
  3694. */
  3695. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  3696. /* If added prefix is link local and we are prepared to process
  3697. router advertisements, start sending router solicitations.
  3698. */
  3699. read_lock_bh(&ifp->idev->lock);
  3700. send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
  3701. send_rs = send_mld &&
  3702. ipv6_accept_ra(ifp->idev) &&
  3703. READ_ONCE(ifp->idev->cnf.rtr_solicits) != 0 &&
  3704. (dev->flags & IFF_LOOPBACK) == 0 &&
  3705. (dev->type != ARPHRD_TUNNEL) &&
  3706. !netif_is_team_port(dev);
  3707. read_unlock_bh(&ifp->idev->lock);
  3708. /* While dad is in progress mld report's source address is in6_addrany.
  3709. * Resend with proper ll now.
  3710. */
  3711. if (send_mld)
  3712. ipv6_mc_dad_complete(ifp->idev);
  3713. /* send unsolicited NA if enabled */
  3714. if (send_na &&
  3715. (READ_ONCE(ifp->idev->cnf.ndisc_notify) ||
  3716. READ_ONCE(dev_net(dev)->ipv6.devconf_all->ndisc_notify))) {
  3717. ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
  3718. /*router=*/ !!ifp->idev->cnf.forwarding,
  3719. /*solicited=*/ false, /*override=*/ true,
  3720. /*inc_opt=*/ true);
  3721. }
  3722. if (send_rs) {
  3723. /*
  3724. * If a host as already performed a random delay
  3725. * [...] as part of DAD [...] there is no need
  3726. * to delay again before sending the first RS
  3727. */
  3728. if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  3729. return;
  3730. ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
  3731. write_lock_bh(&ifp->idev->lock);
  3732. spin_lock(&ifp->lock);
  3733. ifp->idev->rs_interval = rfc3315_s14_backoff_init(
  3734. READ_ONCE(ifp->idev->cnf.rtr_solicit_interval));
  3735. ifp->idev->rs_probes = 1;
  3736. ifp->idev->if_flags |= IF_RS_SENT;
  3737. addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
  3738. spin_unlock(&ifp->lock);
  3739. write_unlock_bh(&ifp->idev->lock);
  3740. }
  3741. if (bump_id)
  3742. rt_genid_bump_ipv6(dev_net(dev));
  3743. /* Make sure that a new temporary address will be created
  3744. * before this temporary address becomes deprecated.
  3745. */
  3746. if (ifp->flags & IFA_F_TEMPORARY)
  3747. addrconf_verify_rtnl(dev_net(dev));
  3748. }
  3749. static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
  3750. {
  3751. struct inet6_ifaddr *ifp;
  3752. read_lock_bh(&idev->lock);
  3753. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  3754. spin_lock(&ifp->lock);
  3755. if ((ifp->flags & IFA_F_TENTATIVE &&
  3756. ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
  3757. if (restart)
  3758. ifp->state = INET6_IFADDR_STATE_PREDAD;
  3759. addrconf_dad_kick(ifp);
  3760. }
  3761. spin_unlock(&ifp->lock);
  3762. }
  3763. read_unlock_bh(&idev->lock);
  3764. }
  3765. #ifdef CONFIG_PROC_FS
  3766. struct if6_iter_state {
  3767. struct seq_net_private p;
  3768. int bucket;
  3769. int offset;
  3770. };
  3771. static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
  3772. {
  3773. struct if6_iter_state *state = seq->private;
  3774. struct net *net = seq_file_net(seq);
  3775. struct inet6_ifaddr *ifa = NULL;
  3776. int p = 0;
  3777. /* initial bucket if pos is 0 */
  3778. if (pos == 0) {
  3779. state->bucket = 0;
  3780. state->offset = 0;
  3781. }
  3782. for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
  3783. hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
  3784. addr_lst) {
  3785. /* sync with offset */
  3786. if (p < state->offset) {
  3787. p++;
  3788. continue;
  3789. }
  3790. return ifa;
  3791. }
  3792. /* prepare for next bucket */
  3793. state->offset = 0;
  3794. p = 0;
  3795. }
  3796. return NULL;
  3797. }
  3798. static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
  3799. struct inet6_ifaddr *ifa)
  3800. {
  3801. struct if6_iter_state *state = seq->private;
  3802. struct net *net = seq_file_net(seq);
  3803. hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
  3804. state->offset++;
  3805. return ifa;
  3806. }
  3807. state->offset = 0;
  3808. while (++state->bucket < IN6_ADDR_HSIZE) {
  3809. hlist_for_each_entry_rcu(ifa,
  3810. &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
  3811. return ifa;
  3812. }
  3813. }
  3814. return NULL;
  3815. }
  3816. static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
  3817. __acquires(rcu)
  3818. {
  3819. rcu_read_lock();
  3820. return if6_get_first(seq, *pos);
  3821. }
  3822. static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3823. {
  3824. struct inet6_ifaddr *ifa;
  3825. ifa = if6_get_next(seq, v);
  3826. ++*pos;
  3827. return ifa;
  3828. }
  3829. static void if6_seq_stop(struct seq_file *seq, void *v)
  3830. __releases(rcu)
  3831. {
  3832. rcu_read_unlock();
  3833. }
  3834. static int if6_seq_show(struct seq_file *seq, void *v)
  3835. {
  3836. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
  3837. seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
  3838. &ifp->addr,
  3839. ifp->idev->dev->ifindex,
  3840. ifp->prefix_len,
  3841. ifp->scope,
  3842. (u8) ifp->flags,
  3843. ifp->idev->dev->name);
  3844. return 0;
  3845. }
  3846. static const struct seq_operations if6_seq_ops = {
  3847. .start = if6_seq_start,
  3848. .next = if6_seq_next,
  3849. .show = if6_seq_show,
  3850. .stop = if6_seq_stop,
  3851. };
  3852. static int __net_init if6_proc_net_init(struct net *net)
  3853. {
  3854. if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
  3855. sizeof(struct if6_iter_state)))
  3856. return -ENOMEM;
  3857. return 0;
  3858. }
  3859. static void __net_exit if6_proc_net_exit(struct net *net)
  3860. {
  3861. remove_proc_entry("if_inet6", net->proc_net);
  3862. }
  3863. static struct pernet_operations if6_proc_net_ops = {
  3864. .init = if6_proc_net_init,
  3865. .exit = if6_proc_net_exit,
  3866. };
  3867. int __init if6_proc_init(void)
  3868. {
  3869. return register_pernet_subsys(&if6_proc_net_ops);
  3870. }
  3871. void if6_proc_exit(void)
  3872. {
  3873. unregister_pernet_subsys(&if6_proc_net_ops);
  3874. }
  3875. #endif /* CONFIG_PROC_FS */
  3876. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  3877. /* Check if address is a home address configured on any interface. */
  3878. int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
  3879. {
  3880. unsigned int hash = inet6_addr_hash(net, addr);
  3881. struct inet6_ifaddr *ifp = NULL;
  3882. int ret = 0;
  3883. rcu_read_lock();
  3884. hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
  3885. if (ipv6_addr_equal(&ifp->addr, addr) &&
  3886. (ifp->flags & IFA_F_HOMEADDRESS)) {
  3887. ret = 1;
  3888. break;
  3889. }
  3890. }
  3891. rcu_read_unlock();
  3892. return ret;
  3893. }
  3894. #endif
  3895. /* RFC6554 has some algorithm to avoid loops in segment routing by
  3896. * checking if the segments contains any of a local interface address.
  3897. *
  3898. * Quote:
  3899. *
  3900. * To detect loops in the SRH, a router MUST determine if the SRH
  3901. * includes multiple addresses assigned to any interface on that router.
  3902. * If such addresses appear more than once and are separated by at least
  3903. * one address not assigned to that router.
  3904. */
  3905. int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
  3906. unsigned char nsegs)
  3907. {
  3908. const struct in6_addr *addr;
  3909. int i, ret = 0, found = 0;
  3910. struct inet6_ifaddr *ifp;
  3911. bool separated = false;
  3912. unsigned int hash;
  3913. bool hash_found;
  3914. rcu_read_lock();
  3915. for (i = 0; i < nsegs; i++) {
  3916. addr = &segs[i];
  3917. hash = inet6_addr_hash(net, addr);
  3918. hash_found = false;
  3919. hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
  3920. if (ipv6_addr_equal(&ifp->addr, addr)) {
  3921. hash_found = true;
  3922. break;
  3923. }
  3924. }
  3925. if (hash_found) {
  3926. if (found > 1 && separated) {
  3927. ret = 1;
  3928. break;
  3929. }
  3930. separated = false;
  3931. found++;
  3932. } else {
  3933. separated = true;
  3934. }
  3935. }
  3936. rcu_read_unlock();
  3937. return ret;
  3938. }
  3939. /*
  3940. * Periodic address status verification
  3941. */
  3942. static void addrconf_verify_rtnl(struct net *net)
  3943. {
  3944. unsigned long now, next, next_sec, next_sched;
  3945. struct inet6_ifaddr *ifp;
  3946. int i;
  3947. ASSERT_RTNL();
  3948. rcu_read_lock_bh();
  3949. now = jiffies;
  3950. next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
  3951. cancel_delayed_work(&net->ipv6.addr_chk_work);
  3952. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  3953. restart:
  3954. hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
  3955. unsigned long age;
  3956. /* When setting preferred_lft to a value not zero or
  3957. * infinity, while valid_lft is infinity
  3958. * IFA_F_PERMANENT has a non-infinity life time.
  3959. */
  3960. if ((ifp->flags & IFA_F_PERMANENT) &&
  3961. (ifp->prefered_lft == INFINITY_LIFE_TIME))
  3962. continue;
  3963. spin_lock(&ifp->lock);
  3964. /* We try to batch several events at once. */
  3965. age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  3966. if ((ifp->flags&IFA_F_TEMPORARY) &&
  3967. !(ifp->flags&IFA_F_TENTATIVE) &&
  3968. ifp->prefered_lft != INFINITY_LIFE_TIME &&
  3969. !ifp->regen_count && ifp->ifpub) {
  3970. /* This is a non-regenerated temporary addr. */
  3971. unsigned long regen_advance = ipv6_get_regen_advance(ifp->idev);
  3972. if (age + regen_advance >= ifp->prefered_lft) {
  3973. struct inet6_ifaddr *ifpub = ifp->ifpub;
  3974. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3975. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3976. ifp->regen_count++;
  3977. in6_ifa_hold(ifp);
  3978. in6_ifa_hold(ifpub);
  3979. spin_unlock(&ifp->lock);
  3980. spin_lock(&ifpub->lock);
  3981. ifpub->regen_count = 0;
  3982. spin_unlock(&ifpub->lock);
  3983. rcu_read_unlock_bh();
  3984. ipv6_create_tempaddr(ifpub, true);
  3985. in6_ifa_put(ifpub);
  3986. in6_ifa_put(ifp);
  3987. rcu_read_lock_bh();
  3988. goto restart;
  3989. } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
  3990. next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
  3991. }
  3992. if (ifp->valid_lft != INFINITY_LIFE_TIME &&
  3993. age >= ifp->valid_lft) {
  3994. spin_unlock(&ifp->lock);
  3995. in6_ifa_hold(ifp);
  3996. rcu_read_unlock_bh();
  3997. ipv6_del_addr(ifp);
  3998. rcu_read_lock_bh();
  3999. goto restart;
  4000. } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
  4001. spin_unlock(&ifp->lock);
  4002. continue;
  4003. } else if (age >= ifp->prefered_lft) {
  4004. /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
  4005. int deprecate = 0;
  4006. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  4007. deprecate = 1;
  4008. ifp->flags |= IFA_F_DEPRECATED;
  4009. }
  4010. if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
  4011. (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
  4012. next = ifp->tstamp + ifp->valid_lft * HZ;
  4013. spin_unlock(&ifp->lock);
  4014. if (deprecate) {
  4015. in6_ifa_hold(ifp);
  4016. ipv6_ifa_notify(0, ifp);
  4017. in6_ifa_put(ifp);
  4018. goto restart;
  4019. }
  4020. } else {
  4021. /* ifp->prefered_lft <= ifp->valid_lft */
  4022. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  4023. next = ifp->tstamp + ifp->prefered_lft * HZ;
  4024. spin_unlock(&ifp->lock);
  4025. }
  4026. }
  4027. }
  4028. next_sec = round_jiffies_up(next);
  4029. next_sched = next;
  4030. /* If rounded timeout is accurate enough, accept it. */
  4031. if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
  4032. next_sched = next_sec;
  4033. /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
  4034. if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
  4035. next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
  4036. pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
  4037. now, next, next_sec, next_sched);
  4038. mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
  4039. rcu_read_unlock_bh();
  4040. }
  4041. static void addrconf_verify_work(struct work_struct *w)
  4042. {
  4043. struct net *net = container_of(to_delayed_work(w), struct net,
  4044. ipv6.addr_chk_work);
  4045. rtnl_lock();
  4046. addrconf_verify_rtnl(net);
  4047. rtnl_unlock();
  4048. }
  4049. static void addrconf_verify(struct net *net)
  4050. {
  4051. mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
  4052. }
  4053. static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
  4054. struct in6_addr **peer_pfx)
  4055. {
  4056. struct in6_addr *pfx = NULL;
  4057. *peer_pfx = NULL;
  4058. if (addr)
  4059. pfx = nla_data(addr);
  4060. if (local) {
  4061. if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
  4062. *peer_pfx = pfx;
  4063. pfx = nla_data(local);
  4064. }
  4065. return pfx;
  4066. }
  4067. static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
  4068. [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
  4069. [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
  4070. [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
  4071. [IFA_FLAGS] = { .len = sizeof(u32) },
  4072. [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
  4073. [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
  4074. [IFA_PROTO] = { .type = NLA_U8 },
  4075. };
  4076. static int
  4077. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
  4078. struct netlink_ext_ack *extack)
  4079. {
  4080. struct net *net = sock_net(skb->sk);
  4081. struct ifaddrmsg *ifm;
  4082. struct nlattr *tb[IFA_MAX+1];
  4083. struct in6_addr *pfx, *peer_pfx;
  4084. u32 ifa_flags;
  4085. int err;
  4086. err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  4087. ifa_ipv6_policy, extack);
  4088. if (err < 0)
  4089. return err;
  4090. ifm = nlmsg_data(nlh);
  4091. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  4092. if (!pfx)
  4093. return -EINVAL;
  4094. ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
  4095. /* We ignore other flags so far. */
  4096. ifa_flags &= IFA_F_MANAGETEMPADDR;
  4097. return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
  4098. ifm->ifa_prefixlen, extack);
  4099. }
  4100. static int modify_prefix_route(struct net *net, struct inet6_ifaddr *ifp,
  4101. unsigned long expires, u32 flags,
  4102. bool modify_peer)
  4103. {
  4104. struct fib6_table *table;
  4105. struct fib6_info *f6i;
  4106. u32 prio;
  4107. f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
  4108. ifp->prefix_len,
  4109. ifp->idev->dev, 0, RTF_DEFAULT, true);
  4110. if (!f6i)
  4111. return -ENOENT;
  4112. prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
  4113. if (f6i->fib6_metric != prio) {
  4114. /* delete old one */
  4115. ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
  4116. /* add new one */
  4117. addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
  4118. ifp->prefix_len,
  4119. ifp->rt_priority, ifp->idev->dev,
  4120. expires, flags, GFP_KERNEL);
  4121. return 0;
  4122. }
  4123. if (f6i != net->ipv6.fib6_null_entry) {
  4124. table = f6i->fib6_table;
  4125. spin_lock_bh(&table->tb6_lock);
  4126. if (!(flags & RTF_EXPIRES)) {
  4127. fib6_clean_expires(f6i);
  4128. fib6_remove_gc_list(f6i);
  4129. } else {
  4130. fib6_set_expires(f6i, expires);
  4131. fib6_add_gc_list(f6i);
  4132. }
  4133. spin_unlock_bh(&table->tb6_lock);
  4134. }
  4135. fib6_info_release(f6i);
  4136. return 0;
  4137. }
  4138. static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
  4139. struct ifa6_config *cfg)
  4140. {
  4141. u32 flags;
  4142. clock_t expires;
  4143. unsigned long timeout;
  4144. bool was_managetempaddr;
  4145. bool had_prefixroute;
  4146. bool new_peer = false;
  4147. ASSERT_RTNL();
  4148. if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
  4149. return -EINVAL;
  4150. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
  4151. (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
  4152. return -EINVAL;
  4153. if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
  4154. cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
  4155. timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
  4156. if (addrconf_finite_timeout(timeout)) {
  4157. expires = jiffies_to_clock_t(timeout * HZ);
  4158. cfg->valid_lft = timeout;
  4159. flags = RTF_EXPIRES;
  4160. } else {
  4161. expires = 0;
  4162. flags = 0;
  4163. cfg->ifa_flags |= IFA_F_PERMANENT;
  4164. }
  4165. timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
  4166. if (addrconf_finite_timeout(timeout)) {
  4167. if (timeout == 0)
  4168. cfg->ifa_flags |= IFA_F_DEPRECATED;
  4169. cfg->preferred_lft = timeout;
  4170. }
  4171. if (cfg->peer_pfx &&
  4172. memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
  4173. if (!ipv6_addr_any(&ifp->peer_addr))
  4174. cleanup_prefix_route(ifp, expires, true, true);
  4175. new_peer = true;
  4176. }
  4177. spin_lock_bh(&ifp->lock);
  4178. was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
  4179. had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
  4180. !(ifp->flags & IFA_F_NOPREFIXROUTE);
  4181. ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
  4182. IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
  4183. IFA_F_NOPREFIXROUTE);
  4184. ifp->flags |= cfg->ifa_flags;
  4185. WRITE_ONCE(ifp->tstamp, jiffies);
  4186. WRITE_ONCE(ifp->valid_lft, cfg->valid_lft);
  4187. WRITE_ONCE(ifp->prefered_lft, cfg->preferred_lft);
  4188. WRITE_ONCE(ifp->ifa_proto, cfg->ifa_proto);
  4189. if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
  4190. WRITE_ONCE(ifp->rt_priority, cfg->rt_priority);
  4191. if (new_peer)
  4192. ifp->peer_addr = *cfg->peer_pfx;
  4193. spin_unlock_bh(&ifp->lock);
  4194. if (!(ifp->flags&IFA_F_TENTATIVE))
  4195. ipv6_ifa_notify(0, ifp);
  4196. if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
  4197. int rc = -ENOENT;
  4198. if (had_prefixroute)
  4199. rc = modify_prefix_route(net, ifp, expires, flags, false);
  4200. /* prefix route could have been deleted; if so restore it */
  4201. if (rc == -ENOENT) {
  4202. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  4203. ifp->rt_priority, ifp->idev->dev,
  4204. expires, flags, GFP_KERNEL);
  4205. }
  4206. if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
  4207. rc = modify_prefix_route(net, ifp, expires, flags, true);
  4208. if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
  4209. addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
  4210. ifp->rt_priority, ifp->idev->dev,
  4211. expires, flags, GFP_KERNEL);
  4212. }
  4213. } else if (had_prefixroute) {
  4214. enum cleanup_prefix_rt_t action;
  4215. unsigned long rt_expires;
  4216. write_lock_bh(&ifp->idev->lock);
  4217. action = check_cleanup_prefix_route(ifp, &rt_expires);
  4218. write_unlock_bh(&ifp->idev->lock);
  4219. if (action != CLEANUP_PREFIX_RT_NOP) {
  4220. cleanup_prefix_route(ifp, rt_expires,
  4221. action == CLEANUP_PREFIX_RT_DEL, false);
  4222. }
  4223. }
  4224. if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
  4225. if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
  4226. delete_tempaddrs(ifp->idev, ifp);
  4227. else
  4228. manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
  4229. cfg->preferred_lft, !was_managetempaddr,
  4230. jiffies);
  4231. }
  4232. addrconf_verify_rtnl(net);
  4233. return 0;
  4234. }
  4235. static int
  4236. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
  4237. struct netlink_ext_ack *extack)
  4238. {
  4239. struct net *net = sock_net(skb->sk);
  4240. struct ifaddrmsg *ifm;
  4241. struct nlattr *tb[IFA_MAX+1];
  4242. struct in6_addr *peer_pfx;
  4243. struct inet6_ifaddr *ifa;
  4244. struct net_device *dev;
  4245. struct inet6_dev *idev;
  4246. struct ifa6_config cfg;
  4247. int err;
  4248. err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  4249. ifa_ipv6_policy, extack);
  4250. if (err < 0)
  4251. return err;
  4252. memset(&cfg, 0, sizeof(cfg));
  4253. ifm = nlmsg_data(nlh);
  4254. cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  4255. if (!cfg.pfx)
  4256. return -EINVAL;
  4257. cfg.peer_pfx = peer_pfx;
  4258. cfg.plen = ifm->ifa_prefixlen;
  4259. if (tb[IFA_RT_PRIORITY])
  4260. cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
  4261. if (tb[IFA_PROTO])
  4262. cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
  4263. cfg.valid_lft = INFINITY_LIFE_TIME;
  4264. cfg.preferred_lft = INFINITY_LIFE_TIME;
  4265. if (tb[IFA_CACHEINFO]) {
  4266. struct ifa_cacheinfo *ci;
  4267. ci = nla_data(tb[IFA_CACHEINFO]);
  4268. cfg.valid_lft = ci->ifa_valid;
  4269. cfg.preferred_lft = ci->ifa_prefered;
  4270. }
  4271. dev = __dev_get_by_index(net, ifm->ifa_index);
  4272. if (!dev) {
  4273. NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
  4274. return -ENODEV;
  4275. }
  4276. if (tb[IFA_FLAGS])
  4277. cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
  4278. else
  4279. cfg.ifa_flags = ifm->ifa_flags;
  4280. /* We ignore other flags so far. */
  4281. cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
  4282. IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
  4283. IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
  4284. idev = ipv6_find_idev(dev);
  4285. if (IS_ERR(idev))
  4286. return PTR_ERR(idev);
  4287. if (!ipv6_allow_optimistic_dad(net, idev))
  4288. cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
  4289. if (cfg.ifa_flags & IFA_F_NODAD &&
  4290. cfg.ifa_flags & IFA_F_OPTIMISTIC) {
  4291. NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
  4292. return -EINVAL;
  4293. }
  4294. ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
  4295. if (!ifa) {
  4296. /*
  4297. * It would be best to check for !NLM_F_CREATE here but
  4298. * userspace already relies on not having to provide this.
  4299. */
  4300. return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
  4301. }
  4302. if (nlh->nlmsg_flags & NLM_F_EXCL ||
  4303. !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
  4304. NL_SET_ERR_MSG_MOD(extack, "address already assigned");
  4305. err = -EEXIST;
  4306. } else {
  4307. err = inet6_addr_modify(net, ifa, &cfg);
  4308. }
  4309. in6_ifa_put(ifa);
  4310. return err;
  4311. }
  4312. static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
  4313. u8 scope, int ifindex)
  4314. {
  4315. struct ifaddrmsg *ifm;
  4316. ifm = nlmsg_data(nlh);
  4317. ifm->ifa_family = AF_INET6;
  4318. ifm->ifa_prefixlen = prefixlen;
  4319. ifm->ifa_flags = flags;
  4320. ifm->ifa_scope = scope;
  4321. ifm->ifa_index = ifindex;
  4322. }
  4323. static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
  4324. unsigned long tstamp, u32 preferred, u32 valid)
  4325. {
  4326. struct ifa_cacheinfo ci;
  4327. ci.cstamp = cstamp_delta(cstamp);
  4328. ci.tstamp = cstamp_delta(tstamp);
  4329. ci.ifa_prefered = preferred;
  4330. ci.ifa_valid = valid;
  4331. return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  4332. }
  4333. static inline int rt_scope(int ifa_scope)
  4334. {
  4335. if (ifa_scope & IFA_HOST)
  4336. return RT_SCOPE_HOST;
  4337. else if (ifa_scope & IFA_LINK)
  4338. return RT_SCOPE_LINK;
  4339. else if (ifa_scope & IFA_SITE)
  4340. return RT_SCOPE_SITE;
  4341. else
  4342. return RT_SCOPE_UNIVERSE;
  4343. }
  4344. static inline int inet6_ifaddr_msgsize(void)
  4345. {
  4346. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  4347. + nla_total_size(16) /* IFA_LOCAL */
  4348. + nla_total_size(16) /* IFA_ADDRESS */
  4349. + nla_total_size(sizeof(struct ifa_cacheinfo))
  4350. + nla_total_size(4) /* IFA_FLAGS */
  4351. + nla_total_size(1) /* IFA_PROTO */
  4352. + nla_total_size(4) /* IFA_RT_PRIORITY */;
  4353. }
  4354. enum addr_type_t {
  4355. UNICAST_ADDR,
  4356. MULTICAST_ADDR,
  4357. ANYCAST_ADDR,
  4358. };
  4359. struct inet6_fill_args {
  4360. u32 portid;
  4361. u32 seq;
  4362. int event;
  4363. unsigned int flags;
  4364. int netnsid;
  4365. int ifindex;
  4366. enum addr_type_t type;
  4367. };
  4368. static int inet6_fill_ifaddr(struct sk_buff *skb,
  4369. const struct inet6_ifaddr *ifa,
  4370. struct inet6_fill_args *args)
  4371. {
  4372. struct nlmsghdr *nlh;
  4373. u32 preferred, valid;
  4374. u32 flags, priority;
  4375. u8 proto;
  4376. nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
  4377. sizeof(struct ifaddrmsg), args->flags);
  4378. if (!nlh)
  4379. return -EMSGSIZE;
  4380. flags = READ_ONCE(ifa->flags);
  4381. put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
  4382. ifa->idev->dev->ifindex);
  4383. if (args->netnsid >= 0 &&
  4384. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
  4385. goto error;
  4386. preferred = READ_ONCE(ifa->prefered_lft);
  4387. valid = READ_ONCE(ifa->valid_lft);
  4388. if (!((flags & IFA_F_PERMANENT) &&
  4389. (preferred == INFINITY_LIFE_TIME))) {
  4390. if (preferred != INFINITY_LIFE_TIME) {
  4391. long tval = (jiffies - READ_ONCE(ifa->tstamp)) / HZ;
  4392. if (preferred > tval)
  4393. preferred -= tval;
  4394. else
  4395. preferred = 0;
  4396. if (valid != INFINITY_LIFE_TIME) {
  4397. if (valid > tval)
  4398. valid -= tval;
  4399. else
  4400. valid = 0;
  4401. }
  4402. }
  4403. } else {
  4404. preferred = INFINITY_LIFE_TIME;
  4405. valid = INFINITY_LIFE_TIME;
  4406. }
  4407. if (!ipv6_addr_any(&ifa->peer_addr)) {
  4408. if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
  4409. nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
  4410. goto error;
  4411. } else {
  4412. if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
  4413. goto error;
  4414. }
  4415. priority = READ_ONCE(ifa->rt_priority);
  4416. if (priority && nla_put_u32(skb, IFA_RT_PRIORITY, priority))
  4417. goto error;
  4418. if (put_cacheinfo(skb, ifa->cstamp, READ_ONCE(ifa->tstamp),
  4419. preferred, valid) < 0)
  4420. goto error;
  4421. if (nla_put_u32(skb, IFA_FLAGS, flags) < 0)
  4422. goto error;
  4423. proto = READ_ONCE(ifa->ifa_proto);
  4424. if (proto && nla_put_u8(skb, IFA_PROTO, proto))
  4425. goto error;
  4426. nlmsg_end(skb, nlh);
  4427. return 0;
  4428. error:
  4429. nlmsg_cancel(skb, nlh);
  4430. return -EMSGSIZE;
  4431. }
  4432. static int inet6_fill_ifmcaddr(struct sk_buff *skb,
  4433. const struct ifmcaddr6 *ifmca,
  4434. struct inet6_fill_args *args)
  4435. {
  4436. int ifindex = ifmca->idev->dev->ifindex;
  4437. u8 scope = RT_SCOPE_UNIVERSE;
  4438. struct nlmsghdr *nlh;
  4439. if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
  4440. scope = RT_SCOPE_SITE;
  4441. nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
  4442. sizeof(struct ifaddrmsg), args->flags);
  4443. if (!nlh)
  4444. return -EMSGSIZE;
  4445. if (args->netnsid >= 0 &&
  4446. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
  4447. nlmsg_cancel(skb, nlh);
  4448. return -EMSGSIZE;
  4449. }
  4450. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  4451. if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
  4452. put_cacheinfo(skb, ifmca->mca_cstamp, READ_ONCE(ifmca->mca_tstamp),
  4453. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  4454. nlmsg_cancel(skb, nlh);
  4455. return -EMSGSIZE;
  4456. }
  4457. nlmsg_end(skb, nlh);
  4458. return 0;
  4459. }
  4460. static int inet6_fill_ifacaddr(struct sk_buff *skb,
  4461. const struct ifacaddr6 *ifaca,
  4462. struct inet6_fill_args *args)
  4463. {
  4464. struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
  4465. int ifindex = dev ? dev->ifindex : 1;
  4466. u8 scope = RT_SCOPE_UNIVERSE;
  4467. struct nlmsghdr *nlh;
  4468. if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
  4469. scope = RT_SCOPE_SITE;
  4470. nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
  4471. sizeof(struct ifaddrmsg), args->flags);
  4472. if (!nlh)
  4473. return -EMSGSIZE;
  4474. if (args->netnsid >= 0 &&
  4475. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
  4476. nlmsg_cancel(skb, nlh);
  4477. return -EMSGSIZE;
  4478. }
  4479. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  4480. if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
  4481. put_cacheinfo(skb, ifaca->aca_cstamp, READ_ONCE(ifaca->aca_tstamp),
  4482. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  4483. nlmsg_cancel(skb, nlh);
  4484. return -EMSGSIZE;
  4485. }
  4486. nlmsg_end(skb, nlh);
  4487. return 0;
  4488. }
  4489. /* called with rcu_read_lock() */
  4490. static int in6_dump_addrs(const struct inet6_dev *idev, struct sk_buff *skb,
  4491. struct netlink_callback *cb, int *s_ip_idx,
  4492. struct inet6_fill_args *fillargs)
  4493. {
  4494. const struct ifmcaddr6 *ifmca;
  4495. const struct ifacaddr6 *ifaca;
  4496. int ip_idx = 0;
  4497. int err = 0;
  4498. switch (fillargs->type) {
  4499. case UNICAST_ADDR: {
  4500. const struct inet6_ifaddr *ifa;
  4501. fillargs->event = RTM_NEWADDR;
  4502. /* unicast address incl. temp addr */
  4503. list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
  4504. if (ip_idx < *s_ip_idx)
  4505. goto next;
  4506. err = inet6_fill_ifaddr(skb, ifa, fillargs);
  4507. if (err < 0)
  4508. break;
  4509. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  4510. next:
  4511. ip_idx++;
  4512. }
  4513. break;
  4514. }
  4515. case MULTICAST_ADDR:
  4516. fillargs->event = RTM_GETMULTICAST;
  4517. /* multicast address */
  4518. for (ifmca = rcu_dereference(idev->mc_list);
  4519. ifmca;
  4520. ifmca = rcu_dereference(ifmca->next), ip_idx++) {
  4521. if (ip_idx < *s_ip_idx)
  4522. continue;
  4523. err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
  4524. if (err < 0)
  4525. break;
  4526. }
  4527. break;
  4528. case ANYCAST_ADDR:
  4529. fillargs->event = RTM_GETANYCAST;
  4530. /* anycast address */
  4531. for (ifaca = rcu_dereference(idev->ac_list); ifaca;
  4532. ifaca = rcu_dereference(ifaca->aca_next), ip_idx++) {
  4533. if (ip_idx < *s_ip_idx)
  4534. continue;
  4535. err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
  4536. if (err < 0)
  4537. break;
  4538. }
  4539. break;
  4540. default:
  4541. break;
  4542. }
  4543. *s_ip_idx = err ? ip_idx : 0;
  4544. return err;
  4545. }
  4546. static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
  4547. struct inet6_fill_args *fillargs,
  4548. struct net **tgt_net, struct sock *sk,
  4549. struct netlink_callback *cb)
  4550. {
  4551. struct netlink_ext_ack *extack = cb->extack;
  4552. struct nlattr *tb[IFA_MAX+1];
  4553. struct ifaddrmsg *ifm;
  4554. int err, i;
  4555. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  4556. NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
  4557. return -EINVAL;
  4558. }
  4559. ifm = nlmsg_data(nlh);
  4560. if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
  4561. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
  4562. return -EINVAL;
  4563. }
  4564. fillargs->ifindex = ifm->ifa_index;
  4565. if (fillargs->ifindex) {
  4566. cb->answer_flags |= NLM_F_DUMP_FILTERED;
  4567. fillargs->flags |= NLM_F_DUMP_FILTERED;
  4568. }
  4569. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
  4570. ifa_ipv6_policy, extack);
  4571. if (err < 0)
  4572. return err;
  4573. for (i = 0; i <= IFA_MAX; ++i) {
  4574. if (!tb[i])
  4575. continue;
  4576. if (i == IFA_TARGET_NETNSID) {
  4577. struct net *net;
  4578. fillargs->netnsid = nla_get_s32(tb[i]);
  4579. net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
  4580. if (IS_ERR(net)) {
  4581. fillargs->netnsid = -1;
  4582. NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
  4583. return PTR_ERR(net);
  4584. }
  4585. *tgt_net = net;
  4586. } else {
  4587. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
  4588. return -EINVAL;
  4589. }
  4590. }
  4591. return 0;
  4592. }
  4593. static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
  4594. enum addr_type_t type)
  4595. {
  4596. struct net *tgt_net = sock_net(skb->sk);
  4597. const struct nlmsghdr *nlh = cb->nlh;
  4598. struct inet6_fill_args fillargs = {
  4599. .portid = NETLINK_CB(cb->skb).portid,
  4600. .seq = cb->nlh->nlmsg_seq,
  4601. .flags = NLM_F_MULTI,
  4602. .netnsid = -1,
  4603. .type = type,
  4604. };
  4605. struct {
  4606. unsigned long ifindex;
  4607. int ip_idx;
  4608. } *ctx = (void *)cb->ctx;
  4609. struct net_device *dev;
  4610. struct inet6_dev *idev;
  4611. int err = 0;
  4612. rcu_read_lock();
  4613. if (cb->strict_check) {
  4614. err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
  4615. skb->sk, cb);
  4616. if (err < 0)
  4617. goto done;
  4618. err = 0;
  4619. if (fillargs.ifindex) {
  4620. dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex);
  4621. if (!dev) {
  4622. err = -ENODEV;
  4623. goto done;
  4624. }
  4625. idev = __in6_dev_get(dev);
  4626. if (idev)
  4627. err = in6_dump_addrs(idev, skb, cb,
  4628. &ctx->ip_idx,
  4629. &fillargs);
  4630. goto done;
  4631. }
  4632. }
  4633. cb->seq = inet6_base_seq(tgt_net);
  4634. for_each_netdev_dump(tgt_net, dev, ctx->ifindex) {
  4635. idev = __in6_dev_get(dev);
  4636. if (!idev)
  4637. continue;
  4638. err = in6_dump_addrs(idev, skb, cb, &ctx->ip_idx,
  4639. &fillargs);
  4640. if (err < 0)
  4641. goto done;
  4642. }
  4643. done:
  4644. rcu_read_unlock();
  4645. if (fillargs.netnsid >= 0)
  4646. put_net(tgt_net);
  4647. return err;
  4648. }
  4649. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4650. {
  4651. enum addr_type_t type = UNICAST_ADDR;
  4652. return inet6_dump_addr(skb, cb, type);
  4653. }
  4654. static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4655. {
  4656. enum addr_type_t type = MULTICAST_ADDR;
  4657. return inet6_dump_addr(skb, cb, type);
  4658. }
  4659. static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4660. {
  4661. enum addr_type_t type = ANYCAST_ADDR;
  4662. return inet6_dump_addr(skb, cb, type);
  4663. }
  4664. static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
  4665. const struct nlmsghdr *nlh,
  4666. struct nlattr **tb,
  4667. struct netlink_ext_ack *extack)
  4668. {
  4669. struct ifaddrmsg *ifm;
  4670. int i, err;
  4671. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  4672. NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
  4673. return -EINVAL;
  4674. }
  4675. if (!netlink_strict_get_check(skb))
  4676. return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  4677. ifa_ipv6_policy, extack);
  4678. ifm = nlmsg_data(nlh);
  4679. if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
  4680. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
  4681. return -EINVAL;
  4682. }
  4683. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
  4684. ifa_ipv6_policy, extack);
  4685. if (err)
  4686. return err;
  4687. for (i = 0; i <= IFA_MAX; i++) {
  4688. if (!tb[i])
  4689. continue;
  4690. switch (i) {
  4691. case IFA_TARGET_NETNSID:
  4692. case IFA_ADDRESS:
  4693. case IFA_LOCAL:
  4694. break;
  4695. default:
  4696. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
  4697. return -EINVAL;
  4698. }
  4699. }
  4700. return 0;
  4701. }
  4702. static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  4703. struct netlink_ext_ack *extack)
  4704. {
  4705. struct net *tgt_net = sock_net(in_skb->sk);
  4706. struct inet6_fill_args fillargs = {
  4707. .portid = NETLINK_CB(in_skb).portid,
  4708. .seq = nlh->nlmsg_seq,
  4709. .event = RTM_NEWADDR,
  4710. .flags = 0,
  4711. .netnsid = -1,
  4712. };
  4713. struct ifaddrmsg *ifm;
  4714. struct nlattr *tb[IFA_MAX+1];
  4715. struct in6_addr *addr = NULL, *peer;
  4716. struct net_device *dev = NULL;
  4717. struct inet6_ifaddr *ifa;
  4718. struct sk_buff *skb;
  4719. int err;
  4720. err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
  4721. if (err < 0)
  4722. return err;
  4723. if (tb[IFA_TARGET_NETNSID]) {
  4724. fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
  4725. tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
  4726. fillargs.netnsid);
  4727. if (IS_ERR(tgt_net))
  4728. return PTR_ERR(tgt_net);
  4729. }
  4730. addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
  4731. if (!addr) {
  4732. err = -EINVAL;
  4733. goto errout;
  4734. }
  4735. ifm = nlmsg_data(nlh);
  4736. if (ifm->ifa_index)
  4737. dev = dev_get_by_index(tgt_net, ifm->ifa_index);
  4738. ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
  4739. if (!ifa) {
  4740. err = -EADDRNOTAVAIL;
  4741. goto errout;
  4742. }
  4743. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
  4744. if (!skb) {
  4745. err = -ENOBUFS;
  4746. goto errout_ifa;
  4747. }
  4748. err = inet6_fill_ifaddr(skb, ifa, &fillargs);
  4749. if (err < 0) {
  4750. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  4751. WARN_ON(err == -EMSGSIZE);
  4752. kfree_skb(skb);
  4753. goto errout_ifa;
  4754. }
  4755. err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
  4756. errout_ifa:
  4757. in6_ifa_put(ifa);
  4758. errout:
  4759. dev_put(dev);
  4760. if (fillargs.netnsid >= 0)
  4761. put_net(tgt_net);
  4762. return err;
  4763. }
  4764. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  4765. {
  4766. struct sk_buff *skb;
  4767. struct net *net = dev_net(ifa->idev->dev);
  4768. struct inet6_fill_args fillargs = {
  4769. .portid = 0,
  4770. .seq = 0,
  4771. .event = event,
  4772. .flags = 0,
  4773. .netnsid = -1,
  4774. };
  4775. int err = -ENOBUFS;
  4776. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
  4777. if (!skb)
  4778. goto errout;
  4779. err = inet6_fill_ifaddr(skb, ifa, &fillargs);
  4780. if (err < 0) {
  4781. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  4782. WARN_ON(err == -EMSGSIZE);
  4783. kfree_skb(skb);
  4784. goto errout;
  4785. }
  4786. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
  4787. return;
  4788. errout:
  4789. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
  4790. }
  4791. static void ipv6_store_devconf(const struct ipv6_devconf *cnf,
  4792. __s32 *array, int bytes)
  4793. {
  4794. BUG_ON(bytes < (DEVCONF_MAX * 4));
  4795. memset(array, 0, bytes);
  4796. array[DEVCONF_FORWARDING] = READ_ONCE(cnf->forwarding);
  4797. array[DEVCONF_HOPLIMIT] = READ_ONCE(cnf->hop_limit);
  4798. array[DEVCONF_MTU6] = READ_ONCE(cnf->mtu6);
  4799. array[DEVCONF_ACCEPT_RA] = READ_ONCE(cnf->accept_ra);
  4800. array[DEVCONF_ACCEPT_REDIRECTS] = READ_ONCE(cnf->accept_redirects);
  4801. array[DEVCONF_AUTOCONF] = READ_ONCE(cnf->autoconf);
  4802. array[DEVCONF_DAD_TRANSMITS] = READ_ONCE(cnf->dad_transmits);
  4803. array[DEVCONF_RTR_SOLICITS] = READ_ONCE(cnf->rtr_solicits);
  4804. array[DEVCONF_RTR_SOLICIT_INTERVAL] =
  4805. jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_interval));
  4806. array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
  4807. jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_max_interval));
  4808. array[DEVCONF_RTR_SOLICIT_DELAY] =
  4809. jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_delay));
  4810. array[DEVCONF_FORCE_MLD_VERSION] = READ_ONCE(cnf->force_mld_version);
  4811. array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
  4812. jiffies_to_msecs(READ_ONCE(cnf->mldv1_unsolicited_report_interval));
  4813. array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
  4814. jiffies_to_msecs(READ_ONCE(cnf->mldv2_unsolicited_report_interval));
  4815. array[DEVCONF_USE_TEMPADDR] = READ_ONCE(cnf->use_tempaddr);
  4816. array[DEVCONF_TEMP_VALID_LFT] = READ_ONCE(cnf->temp_valid_lft);
  4817. array[DEVCONF_TEMP_PREFERED_LFT] = READ_ONCE(cnf->temp_prefered_lft);
  4818. array[DEVCONF_REGEN_MAX_RETRY] = READ_ONCE(cnf->regen_max_retry);
  4819. array[DEVCONF_MAX_DESYNC_FACTOR] = READ_ONCE(cnf->max_desync_factor);
  4820. array[DEVCONF_MAX_ADDRESSES] = READ_ONCE(cnf->max_addresses);
  4821. array[DEVCONF_ACCEPT_RA_DEFRTR] = READ_ONCE(cnf->accept_ra_defrtr);
  4822. array[DEVCONF_RA_DEFRTR_METRIC] = READ_ONCE(cnf->ra_defrtr_metric);
  4823. array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] =
  4824. READ_ONCE(cnf->accept_ra_min_hop_limit);
  4825. array[DEVCONF_ACCEPT_RA_PINFO] = READ_ONCE(cnf->accept_ra_pinfo);
  4826. #ifdef CONFIG_IPV6_ROUTER_PREF
  4827. array[DEVCONF_ACCEPT_RA_RTR_PREF] = READ_ONCE(cnf->accept_ra_rtr_pref);
  4828. array[DEVCONF_RTR_PROBE_INTERVAL] =
  4829. jiffies_to_msecs(READ_ONCE(cnf->rtr_probe_interval));
  4830. #ifdef CONFIG_IPV6_ROUTE_INFO
  4831. array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] =
  4832. READ_ONCE(cnf->accept_ra_rt_info_min_plen);
  4833. array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] =
  4834. READ_ONCE(cnf->accept_ra_rt_info_max_plen);
  4835. #endif
  4836. #endif
  4837. array[DEVCONF_PROXY_NDP] = READ_ONCE(cnf->proxy_ndp);
  4838. array[DEVCONF_ACCEPT_SOURCE_ROUTE] =
  4839. READ_ONCE(cnf->accept_source_route);
  4840. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  4841. array[DEVCONF_OPTIMISTIC_DAD] = READ_ONCE(cnf->optimistic_dad);
  4842. array[DEVCONF_USE_OPTIMISTIC] = READ_ONCE(cnf->use_optimistic);
  4843. #endif
  4844. #ifdef CONFIG_IPV6_MROUTE
  4845. array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
  4846. #endif
  4847. array[DEVCONF_DISABLE_IPV6] = READ_ONCE(cnf->disable_ipv6);
  4848. array[DEVCONF_ACCEPT_DAD] = READ_ONCE(cnf->accept_dad);
  4849. array[DEVCONF_FORCE_TLLAO] = READ_ONCE(cnf->force_tllao);
  4850. array[DEVCONF_NDISC_NOTIFY] = READ_ONCE(cnf->ndisc_notify);
  4851. array[DEVCONF_SUPPRESS_FRAG_NDISC] =
  4852. READ_ONCE(cnf->suppress_frag_ndisc);
  4853. array[DEVCONF_ACCEPT_RA_FROM_LOCAL] =
  4854. READ_ONCE(cnf->accept_ra_from_local);
  4855. array[DEVCONF_ACCEPT_RA_MTU] = READ_ONCE(cnf->accept_ra_mtu);
  4856. array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] =
  4857. READ_ONCE(cnf->ignore_routes_with_linkdown);
  4858. /* we omit DEVCONF_STABLE_SECRET for now */
  4859. array[DEVCONF_USE_OIF_ADDRS_ONLY] = READ_ONCE(cnf->use_oif_addrs_only);
  4860. array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] =
  4861. READ_ONCE(cnf->drop_unicast_in_l2_multicast);
  4862. array[DEVCONF_DROP_UNSOLICITED_NA] = READ_ONCE(cnf->drop_unsolicited_na);
  4863. array[DEVCONF_KEEP_ADDR_ON_DOWN] = READ_ONCE(cnf->keep_addr_on_down);
  4864. array[DEVCONF_SEG6_ENABLED] = READ_ONCE(cnf->seg6_enabled);
  4865. #ifdef CONFIG_IPV6_SEG6_HMAC
  4866. array[DEVCONF_SEG6_REQUIRE_HMAC] = READ_ONCE(cnf->seg6_require_hmac);
  4867. #endif
  4868. array[DEVCONF_ENHANCED_DAD] = READ_ONCE(cnf->enhanced_dad);
  4869. array[DEVCONF_ADDR_GEN_MODE] = READ_ONCE(cnf->addr_gen_mode);
  4870. array[DEVCONF_DISABLE_POLICY] = READ_ONCE(cnf->disable_policy);
  4871. array[DEVCONF_NDISC_TCLASS] = READ_ONCE(cnf->ndisc_tclass);
  4872. array[DEVCONF_RPL_SEG_ENABLED] = READ_ONCE(cnf->rpl_seg_enabled);
  4873. array[DEVCONF_IOAM6_ENABLED] = READ_ONCE(cnf->ioam6_enabled);
  4874. array[DEVCONF_IOAM6_ID] = READ_ONCE(cnf->ioam6_id);
  4875. array[DEVCONF_IOAM6_ID_WIDE] = READ_ONCE(cnf->ioam6_id_wide);
  4876. array[DEVCONF_NDISC_EVICT_NOCARRIER] =
  4877. READ_ONCE(cnf->ndisc_evict_nocarrier);
  4878. array[DEVCONF_ACCEPT_UNTRACKED_NA] =
  4879. READ_ONCE(cnf->accept_untracked_na);
  4880. array[DEVCONF_ACCEPT_RA_MIN_LFT] = READ_ONCE(cnf->accept_ra_min_lft);
  4881. }
  4882. static inline size_t inet6_ifla6_size(void)
  4883. {
  4884. return nla_total_size(4) /* IFLA_INET6_FLAGS */
  4885. + nla_total_size(sizeof(struct ifla_cacheinfo))
  4886. + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
  4887. + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
  4888. + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
  4889. + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
  4890. + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
  4891. + nla_total_size(4) /* IFLA_INET6_RA_MTU */
  4892. + 0;
  4893. }
  4894. static inline size_t inet6_if_nlmsg_size(void)
  4895. {
  4896. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  4897. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  4898. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  4899. + nla_total_size(4) /* IFLA_MTU */
  4900. + nla_total_size(4) /* IFLA_LINK */
  4901. + nla_total_size(1) /* IFLA_OPERSTATE */
  4902. + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
  4903. }
  4904. static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
  4905. int bytes)
  4906. {
  4907. int i;
  4908. int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
  4909. BUG_ON(pad < 0);
  4910. /* Use put_unaligned() because stats may not be aligned for u64. */
  4911. put_unaligned(ICMP6_MIB_MAX, &stats[0]);
  4912. for (i = 1; i < ICMP6_MIB_MAX; i++)
  4913. put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
  4914. memset(&stats[ICMP6_MIB_MAX], 0, pad);
  4915. }
  4916. static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
  4917. int bytes, size_t syncpoff)
  4918. {
  4919. int i, c;
  4920. u64 buff[IPSTATS_MIB_MAX];
  4921. int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
  4922. BUG_ON(pad < 0);
  4923. memset(buff, 0, sizeof(buff));
  4924. buff[0] = IPSTATS_MIB_MAX;
  4925. for_each_possible_cpu(c) {
  4926. for (i = 1; i < IPSTATS_MIB_MAX; i++)
  4927. buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
  4928. }
  4929. memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
  4930. memset(&stats[IPSTATS_MIB_MAX], 0, pad);
  4931. }
  4932. static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
  4933. int bytes)
  4934. {
  4935. switch (attrtype) {
  4936. case IFLA_INET6_STATS:
  4937. __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
  4938. offsetof(struct ipstats_mib, syncp));
  4939. break;
  4940. case IFLA_INET6_ICMP6STATS:
  4941. __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
  4942. break;
  4943. }
  4944. }
  4945. static int inet6_fill_ifla6_stats_attrs(struct sk_buff *skb,
  4946. struct inet6_dev *idev)
  4947. {
  4948. struct nlattr *nla;
  4949. nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
  4950. if (!nla)
  4951. goto nla_put_failure;
  4952. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
  4953. nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
  4954. if (!nla)
  4955. goto nla_put_failure;
  4956. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
  4957. return 0;
  4958. nla_put_failure:
  4959. return -EMSGSIZE;
  4960. }
  4961. static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
  4962. u32 ext_filter_mask)
  4963. {
  4964. struct ifla_cacheinfo ci;
  4965. struct nlattr *nla;
  4966. u32 ra_mtu;
  4967. if (nla_put_u32(skb, IFLA_INET6_FLAGS, READ_ONCE(idev->if_flags)))
  4968. goto nla_put_failure;
  4969. ci.max_reasm_len = IPV6_MAXPLEN;
  4970. ci.tstamp = cstamp_delta(READ_ONCE(idev->tstamp));
  4971. ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
  4972. ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
  4973. if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
  4974. goto nla_put_failure;
  4975. nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
  4976. if (!nla)
  4977. goto nla_put_failure;
  4978. ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
  4979. /* XXX - MC not implemented */
  4980. if (!(ext_filter_mask & RTEXT_FILTER_SKIP_STATS)) {
  4981. if (inet6_fill_ifla6_stats_attrs(skb, idev) < 0)
  4982. goto nla_put_failure;
  4983. }
  4984. nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
  4985. if (!nla)
  4986. goto nla_put_failure;
  4987. read_lock_bh(&idev->lock);
  4988. memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
  4989. read_unlock_bh(&idev->lock);
  4990. if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE,
  4991. READ_ONCE(idev->cnf.addr_gen_mode)))
  4992. goto nla_put_failure;
  4993. ra_mtu = READ_ONCE(idev->ra_mtu);
  4994. if (ra_mtu && nla_put_u32(skb, IFLA_INET6_RA_MTU, ra_mtu))
  4995. goto nla_put_failure;
  4996. return 0;
  4997. nla_put_failure:
  4998. return -EMSGSIZE;
  4999. }
  5000. static size_t inet6_get_link_af_size(const struct net_device *dev,
  5001. u32 ext_filter_mask)
  5002. {
  5003. if (!__in6_dev_get(dev))
  5004. return 0;
  5005. return inet6_ifla6_size();
  5006. }
  5007. static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
  5008. u32 ext_filter_mask)
  5009. {
  5010. struct inet6_dev *idev = __in6_dev_get(dev);
  5011. if (!idev)
  5012. return -ENODATA;
  5013. if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
  5014. return -EMSGSIZE;
  5015. return 0;
  5016. }
  5017. static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
  5018. struct netlink_ext_ack *extack)
  5019. {
  5020. struct inet6_ifaddr *ifp;
  5021. struct net_device *dev = idev->dev;
  5022. bool clear_token, update_rs = false;
  5023. struct in6_addr ll_addr;
  5024. ASSERT_RTNL();
  5025. if (!token)
  5026. return -EINVAL;
  5027. if (dev->flags & IFF_LOOPBACK) {
  5028. NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
  5029. return -EINVAL;
  5030. }
  5031. if (dev->flags & IFF_NOARP) {
  5032. NL_SET_ERR_MSG_MOD(extack,
  5033. "Device does not do neighbour discovery");
  5034. return -EINVAL;
  5035. }
  5036. if (!ipv6_accept_ra(idev)) {
  5037. NL_SET_ERR_MSG_MOD(extack,
  5038. "Router advertisement is disabled on device");
  5039. return -EINVAL;
  5040. }
  5041. if (READ_ONCE(idev->cnf.rtr_solicits) == 0) {
  5042. NL_SET_ERR_MSG(extack,
  5043. "Router solicitation is disabled on device");
  5044. return -EINVAL;
  5045. }
  5046. write_lock_bh(&idev->lock);
  5047. BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
  5048. memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
  5049. write_unlock_bh(&idev->lock);
  5050. clear_token = ipv6_addr_any(token);
  5051. if (clear_token)
  5052. goto update_lft;
  5053. if (!idev->dead && (idev->if_flags & IF_READY) &&
  5054. !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
  5055. IFA_F_OPTIMISTIC)) {
  5056. /* If we're not ready, then normal ifup will take care
  5057. * of this. Otherwise, we need to request our rs here.
  5058. */
  5059. ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
  5060. update_rs = true;
  5061. }
  5062. update_lft:
  5063. write_lock_bh(&idev->lock);
  5064. if (update_rs) {
  5065. idev->if_flags |= IF_RS_SENT;
  5066. idev->rs_interval = rfc3315_s14_backoff_init(
  5067. READ_ONCE(idev->cnf.rtr_solicit_interval));
  5068. idev->rs_probes = 1;
  5069. addrconf_mod_rs_timer(idev, idev->rs_interval);
  5070. }
  5071. /* Well, that's kinda nasty ... */
  5072. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  5073. spin_lock(&ifp->lock);
  5074. if (ifp->tokenized) {
  5075. ifp->valid_lft = 0;
  5076. ifp->prefered_lft = 0;
  5077. }
  5078. spin_unlock(&ifp->lock);
  5079. }
  5080. write_unlock_bh(&idev->lock);
  5081. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  5082. addrconf_verify_rtnl(dev_net(dev));
  5083. return 0;
  5084. }
  5085. static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
  5086. [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
  5087. [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
  5088. [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT,
  5089. .reject_message =
  5090. "IFLA_INET6_RA_MTU can not be set" },
  5091. };
  5092. static int check_addr_gen_mode(int mode)
  5093. {
  5094. if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
  5095. mode != IN6_ADDR_GEN_MODE_NONE &&
  5096. mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
  5097. mode != IN6_ADDR_GEN_MODE_RANDOM)
  5098. return -EINVAL;
  5099. return 1;
  5100. }
  5101. static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
  5102. int mode)
  5103. {
  5104. if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
  5105. !idev->cnf.stable_secret.initialized &&
  5106. !net->ipv6.devconf_dflt->stable_secret.initialized)
  5107. return -EINVAL;
  5108. return 1;
  5109. }
  5110. static int inet6_validate_link_af(const struct net_device *dev,
  5111. const struct nlattr *nla,
  5112. struct netlink_ext_ack *extack)
  5113. {
  5114. struct nlattr *tb[IFLA_INET6_MAX + 1];
  5115. struct inet6_dev *idev = NULL;
  5116. int err;
  5117. if (dev) {
  5118. idev = __in6_dev_get(dev);
  5119. if (!idev)
  5120. return -EAFNOSUPPORT;
  5121. }
  5122. err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
  5123. inet6_af_policy, extack);
  5124. if (err)
  5125. return err;
  5126. if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
  5127. return -EINVAL;
  5128. if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
  5129. u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
  5130. if (check_addr_gen_mode(mode) < 0)
  5131. return -EINVAL;
  5132. if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
  5133. return -EINVAL;
  5134. }
  5135. return 0;
  5136. }
  5137. static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
  5138. struct netlink_ext_ack *extack)
  5139. {
  5140. struct inet6_dev *idev = __in6_dev_get(dev);
  5141. struct nlattr *tb[IFLA_INET6_MAX + 1];
  5142. int err;
  5143. if (!idev)
  5144. return -EAFNOSUPPORT;
  5145. if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
  5146. return -EINVAL;
  5147. if (tb[IFLA_INET6_TOKEN]) {
  5148. err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
  5149. extack);
  5150. if (err)
  5151. return err;
  5152. }
  5153. if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
  5154. u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
  5155. WRITE_ONCE(idev->cnf.addr_gen_mode, mode);
  5156. }
  5157. return 0;
  5158. }
  5159. static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
  5160. u32 portid, u32 seq, int event, unsigned int flags)
  5161. {
  5162. struct net_device *dev = idev->dev;
  5163. struct ifinfomsg *hdr;
  5164. struct nlmsghdr *nlh;
  5165. int ifindex, iflink;
  5166. void *protoinfo;
  5167. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
  5168. if (!nlh)
  5169. return -EMSGSIZE;
  5170. hdr = nlmsg_data(nlh);
  5171. hdr->ifi_family = AF_INET6;
  5172. hdr->__ifi_pad = 0;
  5173. hdr->ifi_type = dev->type;
  5174. ifindex = READ_ONCE(dev->ifindex);
  5175. hdr->ifi_index = ifindex;
  5176. hdr->ifi_flags = dev_get_flags(dev);
  5177. hdr->ifi_change = 0;
  5178. iflink = dev_get_iflink(dev);
  5179. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  5180. (dev->addr_len &&
  5181. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  5182. nla_put_u32(skb, IFLA_MTU, READ_ONCE(dev->mtu)) ||
  5183. (ifindex != iflink &&
  5184. nla_put_u32(skb, IFLA_LINK, iflink)) ||
  5185. nla_put_u8(skb, IFLA_OPERSTATE,
  5186. netif_running(dev) ? READ_ONCE(dev->operstate) : IF_OPER_DOWN))
  5187. goto nla_put_failure;
  5188. protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
  5189. if (!protoinfo)
  5190. goto nla_put_failure;
  5191. if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
  5192. goto nla_put_failure;
  5193. nla_nest_end(skb, protoinfo);
  5194. nlmsg_end(skb, nlh);
  5195. return 0;
  5196. nla_put_failure:
  5197. nlmsg_cancel(skb, nlh);
  5198. return -EMSGSIZE;
  5199. }
  5200. static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
  5201. struct netlink_ext_ack *extack)
  5202. {
  5203. struct ifinfomsg *ifm;
  5204. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  5205. NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
  5206. return -EINVAL;
  5207. }
  5208. if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
  5209. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
  5210. return -EINVAL;
  5211. }
  5212. ifm = nlmsg_data(nlh);
  5213. if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
  5214. ifm->ifi_change || ifm->ifi_index) {
  5215. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
  5216. return -EINVAL;
  5217. }
  5218. return 0;
  5219. }
  5220. static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  5221. {
  5222. struct net *net = sock_net(skb->sk);
  5223. struct {
  5224. unsigned long ifindex;
  5225. } *ctx = (void *)cb->ctx;
  5226. struct net_device *dev;
  5227. struct inet6_dev *idev;
  5228. int err;
  5229. /* only requests using strict checking can pass data to
  5230. * influence the dump
  5231. */
  5232. if (cb->strict_check) {
  5233. err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
  5234. if (err < 0)
  5235. return err;
  5236. }
  5237. err = 0;
  5238. rcu_read_lock();
  5239. for_each_netdev_dump(net, dev, ctx->ifindex) {
  5240. idev = __in6_dev_get(dev);
  5241. if (!idev)
  5242. continue;
  5243. err = inet6_fill_ifinfo(skb, idev,
  5244. NETLINK_CB(cb->skb).portid,
  5245. cb->nlh->nlmsg_seq,
  5246. RTM_NEWLINK, NLM_F_MULTI);
  5247. if (err < 0)
  5248. break;
  5249. }
  5250. rcu_read_unlock();
  5251. return err;
  5252. }
  5253. void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
  5254. {
  5255. struct sk_buff *skb;
  5256. struct net *net = dev_net(idev->dev);
  5257. int err = -ENOBUFS;
  5258. skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
  5259. if (!skb)
  5260. goto errout;
  5261. err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
  5262. if (err < 0) {
  5263. /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
  5264. WARN_ON(err == -EMSGSIZE);
  5265. kfree_skb(skb);
  5266. goto errout;
  5267. }
  5268. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
  5269. return;
  5270. errout:
  5271. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
  5272. }
  5273. static inline size_t inet6_prefix_nlmsg_size(void)
  5274. {
  5275. return NLMSG_ALIGN(sizeof(struct prefixmsg))
  5276. + nla_total_size(sizeof(struct in6_addr))
  5277. + nla_total_size(sizeof(struct prefix_cacheinfo));
  5278. }
  5279. static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
  5280. struct prefix_info *pinfo, u32 portid, u32 seq,
  5281. int event, unsigned int flags)
  5282. {
  5283. struct prefixmsg *pmsg;
  5284. struct nlmsghdr *nlh;
  5285. struct prefix_cacheinfo ci;
  5286. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
  5287. if (!nlh)
  5288. return -EMSGSIZE;
  5289. pmsg = nlmsg_data(nlh);
  5290. pmsg->prefix_family = AF_INET6;
  5291. pmsg->prefix_pad1 = 0;
  5292. pmsg->prefix_pad2 = 0;
  5293. pmsg->prefix_ifindex = idev->dev->ifindex;
  5294. pmsg->prefix_len = pinfo->prefix_len;
  5295. pmsg->prefix_type = pinfo->type;
  5296. pmsg->prefix_pad3 = 0;
  5297. pmsg->prefix_flags = pinfo->flags;
  5298. if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
  5299. goto nla_put_failure;
  5300. ci.preferred_time = ntohl(pinfo->prefered);
  5301. ci.valid_time = ntohl(pinfo->valid);
  5302. if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
  5303. goto nla_put_failure;
  5304. nlmsg_end(skb, nlh);
  5305. return 0;
  5306. nla_put_failure:
  5307. nlmsg_cancel(skb, nlh);
  5308. return -EMSGSIZE;
  5309. }
  5310. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  5311. struct prefix_info *pinfo)
  5312. {
  5313. struct sk_buff *skb;
  5314. struct net *net = dev_net(idev->dev);
  5315. int err = -ENOBUFS;
  5316. skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
  5317. if (!skb)
  5318. goto errout;
  5319. err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
  5320. if (err < 0) {
  5321. /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
  5322. WARN_ON(err == -EMSGSIZE);
  5323. kfree_skb(skb);
  5324. goto errout;
  5325. }
  5326. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
  5327. return;
  5328. errout:
  5329. rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
  5330. }
  5331. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  5332. {
  5333. struct net *net = dev_net(ifp->idev->dev);
  5334. if (event)
  5335. ASSERT_RTNL();
  5336. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  5337. switch (event) {
  5338. case RTM_NEWADDR:
  5339. /*
  5340. * If the address was optimistic we inserted the route at the
  5341. * start of our DAD process, so we don't need to do it again.
  5342. * If the device was taken down in the middle of the DAD
  5343. * cycle there is a race where we could get here without a
  5344. * host route, so nothing to insert. That will be fixed when
  5345. * the device is brought up.
  5346. */
  5347. if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
  5348. ip6_ins_rt(net, ifp->rt);
  5349. } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
  5350. pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
  5351. &ifp->addr, ifp->idev->dev->name);
  5352. }
  5353. if (ifp->idev->cnf.forwarding)
  5354. addrconf_join_anycast(ifp);
  5355. if (!ipv6_addr_any(&ifp->peer_addr))
  5356. addrconf_prefix_route(&ifp->peer_addr, 128,
  5357. ifp->rt_priority, ifp->idev->dev,
  5358. 0, 0, GFP_ATOMIC);
  5359. break;
  5360. case RTM_DELADDR:
  5361. if (ifp->idev->cnf.forwarding)
  5362. addrconf_leave_anycast(ifp);
  5363. addrconf_leave_solict(ifp->idev, &ifp->addr);
  5364. if (!ipv6_addr_any(&ifp->peer_addr)) {
  5365. struct fib6_info *rt;
  5366. rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
  5367. ifp->idev->dev, 0, 0,
  5368. false);
  5369. if (rt)
  5370. ip6_del_rt(net, rt, false);
  5371. }
  5372. if (ifp->rt) {
  5373. ip6_del_rt(net, ifp->rt, false);
  5374. ifp->rt = NULL;
  5375. }
  5376. rt_genid_bump_ipv6(net);
  5377. break;
  5378. }
  5379. atomic_inc(&net->ipv6.dev_addr_genid);
  5380. }
  5381. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  5382. {
  5383. if (likely(ifp->idev->dead == 0))
  5384. __ipv6_ifa_notify(event, ifp);
  5385. }
  5386. #ifdef CONFIG_SYSCTL
  5387. static int addrconf_sysctl_forward(const struct ctl_table *ctl, int write,
  5388. void *buffer, size_t *lenp, loff_t *ppos)
  5389. {
  5390. int *valp = ctl->data;
  5391. int val = *valp;
  5392. loff_t pos = *ppos;
  5393. struct ctl_table lctl;
  5394. int ret;
  5395. /*
  5396. * ctl->data points to idev->cnf.forwarding, we should
  5397. * not modify it until we get the rtnl lock.
  5398. */
  5399. lctl = *ctl;
  5400. lctl.data = &val;
  5401. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5402. if (write)
  5403. ret = addrconf_fixup_forwarding(ctl, valp, val);
  5404. if (ret)
  5405. *ppos = pos;
  5406. return ret;
  5407. }
  5408. static int addrconf_sysctl_mtu(const struct ctl_table *ctl, int write,
  5409. void *buffer, size_t *lenp, loff_t *ppos)
  5410. {
  5411. struct inet6_dev *idev = ctl->extra1;
  5412. int min_mtu = IPV6_MIN_MTU;
  5413. struct ctl_table lctl;
  5414. lctl = *ctl;
  5415. lctl.extra1 = &min_mtu;
  5416. lctl.extra2 = idev ? &idev->dev->mtu : NULL;
  5417. return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
  5418. }
  5419. static void dev_disable_change(struct inet6_dev *idev)
  5420. {
  5421. struct netdev_notifier_info info;
  5422. if (!idev || !idev->dev)
  5423. return;
  5424. netdev_notifier_info_init(&info, idev->dev);
  5425. if (idev->cnf.disable_ipv6)
  5426. addrconf_notify(NULL, NETDEV_DOWN, &info);
  5427. else
  5428. addrconf_notify(NULL, NETDEV_UP, &info);
  5429. }
  5430. static void addrconf_disable_change(struct net *net, __s32 newf)
  5431. {
  5432. struct net_device *dev;
  5433. struct inet6_dev *idev;
  5434. for_each_netdev(net, dev) {
  5435. idev = __in6_dev_get(dev);
  5436. if (idev) {
  5437. int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
  5438. WRITE_ONCE(idev->cnf.disable_ipv6, newf);
  5439. if (changed)
  5440. dev_disable_change(idev);
  5441. }
  5442. }
  5443. }
  5444. static int addrconf_disable_ipv6(const struct ctl_table *table, int *p, int newf)
  5445. {
  5446. struct net *net = (struct net *)table->extra2;
  5447. int old;
  5448. if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
  5449. WRITE_ONCE(*p, newf);
  5450. return 0;
  5451. }
  5452. if (!rtnl_trylock())
  5453. return restart_syscall();
  5454. old = *p;
  5455. WRITE_ONCE(*p, newf);
  5456. if (p == &net->ipv6.devconf_all->disable_ipv6) {
  5457. WRITE_ONCE(net->ipv6.devconf_dflt->disable_ipv6, newf);
  5458. addrconf_disable_change(net, newf);
  5459. } else if ((!newf) ^ (!old))
  5460. dev_disable_change((struct inet6_dev *)table->extra1);
  5461. rtnl_unlock();
  5462. return 0;
  5463. }
  5464. static int addrconf_sysctl_disable(const struct ctl_table *ctl, int write,
  5465. void *buffer, size_t *lenp, loff_t *ppos)
  5466. {
  5467. int *valp = ctl->data;
  5468. int val = *valp;
  5469. loff_t pos = *ppos;
  5470. struct ctl_table lctl;
  5471. int ret;
  5472. /*
  5473. * ctl->data points to idev->cnf.disable_ipv6, we should
  5474. * not modify it until we get the rtnl lock.
  5475. */
  5476. lctl = *ctl;
  5477. lctl.data = &val;
  5478. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5479. if (write)
  5480. ret = addrconf_disable_ipv6(ctl, valp, val);
  5481. if (ret)
  5482. *ppos = pos;
  5483. return ret;
  5484. }
  5485. static int addrconf_sysctl_proxy_ndp(const struct ctl_table *ctl, int write,
  5486. void *buffer, size_t *lenp, loff_t *ppos)
  5487. {
  5488. int *valp = ctl->data;
  5489. int ret;
  5490. int old, new;
  5491. old = *valp;
  5492. ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  5493. new = *valp;
  5494. if (write && old != new) {
  5495. struct net *net = ctl->extra2;
  5496. if (!rtnl_trylock())
  5497. return restart_syscall();
  5498. if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
  5499. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5500. NETCONFA_PROXY_NEIGH,
  5501. NETCONFA_IFINDEX_DEFAULT,
  5502. net->ipv6.devconf_dflt);
  5503. else if (valp == &net->ipv6.devconf_all->proxy_ndp)
  5504. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5505. NETCONFA_PROXY_NEIGH,
  5506. NETCONFA_IFINDEX_ALL,
  5507. net->ipv6.devconf_all);
  5508. else {
  5509. struct inet6_dev *idev = ctl->extra1;
  5510. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5511. NETCONFA_PROXY_NEIGH,
  5512. idev->dev->ifindex,
  5513. &idev->cnf);
  5514. }
  5515. rtnl_unlock();
  5516. }
  5517. return ret;
  5518. }
  5519. static int addrconf_sysctl_addr_gen_mode(const struct ctl_table *ctl, int write,
  5520. void *buffer, size_t *lenp,
  5521. loff_t *ppos)
  5522. {
  5523. int ret = 0;
  5524. u32 new_val;
  5525. struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
  5526. struct net *net = (struct net *)ctl->extra2;
  5527. struct ctl_table tmp = {
  5528. .data = &new_val,
  5529. .maxlen = sizeof(new_val),
  5530. .mode = ctl->mode,
  5531. };
  5532. if (!rtnl_trylock())
  5533. return restart_syscall();
  5534. new_val = *((u32 *)ctl->data);
  5535. ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
  5536. if (ret != 0)
  5537. goto out;
  5538. if (write) {
  5539. if (check_addr_gen_mode(new_val) < 0) {
  5540. ret = -EINVAL;
  5541. goto out;
  5542. }
  5543. if (idev) {
  5544. if (check_stable_privacy(idev, net, new_val) < 0) {
  5545. ret = -EINVAL;
  5546. goto out;
  5547. }
  5548. if (idev->cnf.addr_gen_mode != new_val) {
  5549. WRITE_ONCE(idev->cnf.addr_gen_mode, new_val);
  5550. addrconf_init_auto_addrs(idev->dev);
  5551. }
  5552. } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
  5553. struct net_device *dev;
  5554. WRITE_ONCE(net->ipv6.devconf_dflt->addr_gen_mode, new_val);
  5555. for_each_netdev(net, dev) {
  5556. idev = __in6_dev_get(dev);
  5557. if (idev &&
  5558. idev->cnf.addr_gen_mode != new_val) {
  5559. WRITE_ONCE(idev->cnf.addr_gen_mode,
  5560. new_val);
  5561. addrconf_init_auto_addrs(idev->dev);
  5562. }
  5563. }
  5564. }
  5565. WRITE_ONCE(*((u32 *)ctl->data), new_val);
  5566. }
  5567. out:
  5568. rtnl_unlock();
  5569. return ret;
  5570. }
  5571. static int addrconf_sysctl_stable_secret(const struct ctl_table *ctl, int write,
  5572. void *buffer, size_t *lenp,
  5573. loff_t *ppos)
  5574. {
  5575. int err;
  5576. struct in6_addr addr;
  5577. char str[IPV6_MAX_STRLEN];
  5578. struct ctl_table lctl = *ctl;
  5579. struct net *net = ctl->extra2;
  5580. struct ipv6_stable_secret *secret = ctl->data;
  5581. if (&net->ipv6.devconf_all->stable_secret == ctl->data)
  5582. return -EIO;
  5583. lctl.maxlen = IPV6_MAX_STRLEN;
  5584. lctl.data = str;
  5585. if (!rtnl_trylock())
  5586. return restart_syscall();
  5587. if (!write && !secret->initialized) {
  5588. err = -EIO;
  5589. goto out;
  5590. }
  5591. err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
  5592. if (err >= sizeof(str)) {
  5593. err = -EIO;
  5594. goto out;
  5595. }
  5596. err = proc_dostring(&lctl, write, buffer, lenp, ppos);
  5597. if (err || !write)
  5598. goto out;
  5599. if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
  5600. err = -EIO;
  5601. goto out;
  5602. }
  5603. secret->initialized = true;
  5604. secret->secret = addr;
  5605. if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
  5606. struct net_device *dev;
  5607. for_each_netdev(net, dev) {
  5608. struct inet6_dev *idev = __in6_dev_get(dev);
  5609. if (idev) {
  5610. WRITE_ONCE(idev->cnf.addr_gen_mode,
  5611. IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
  5612. }
  5613. }
  5614. } else {
  5615. struct inet6_dev *idev = ctl->extra1;
  5616. WRITE_ONCE(idev->cnf.addr_gen_mode,
  5617. IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
  5618. }
  5619. out:
  5620. rtnl_unlock();
  5621. return err;
  5622. }
  5623. static
  5624. int addrconf_sysctl_ignore_routes_with_linkdown(const struct ctl_table *ctl,
  5625. int write, void *buffer,
  5626. size_t *lenp,
  5627. loff_t *ppos)
  5628. {
  5629. int *valp = ctl->data;
  5630. int val = *valp;
  5631. loff_t pos = *ppos;
  5632. struct ctl_table lctl;
  5633. int ret;
  5634. /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
  5635. * we should not modify it until we get the rtnl lock.
  5636. */
  5637. lctl = *ctl;
  5638. lctl.data = &val;
  5639. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5640. if (write)
  5641. ret = addrconf_fixup_linkdown(ctl, valp, val);
  5642. if (ret)
  5643. *ppos = pos;
  5644. return ret;
  5645. }
  5646. static
  5647. void addrconf_set_nopolicy(struct rt6_info *rt, int action)
  5648. {
  5649. if (rt) {
  5650. if (action)
  5651. rt->dst.flags |= DST_NOPOLICY;
  5652. else
  5653. rt->dst.flags &= ~DST_NOPOLICY;
  5654. }
  5655. }
  5656. static
  5657. void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
  5658. {
  5659. struct inet6_ifaddr *ifa;
  5660. read_lock_bh(&idev->lock);
  5661. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  5662. spin_lock(&ifa->lock);
  5663. if (ifa->rt) {
  5664. /* host routes only use builtin fib6_nh */
  5665. struct fib6_nh *nh = ifa->rt->fib6_nh;
  5666. int cpu;
  5667. rcu_read_lock();
  5668. ifa->rt->dst_nopolicy = val ? true : false;
  5669. if (nh->rt6i_pcpu) {
  5670. for_each_possible_cpu(cpu) {
  5671. struct rt6_info **rtp;
  5672. rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
  5673. addrconf_set_nopolicy(*rtp, val);
  5674. }
  5675. }
  5676. rcu_read_unlock();
  5677. }
  5678. spin_unlock(&ifa->lock);
  5679. }
  5680. read_unlock_bh(&idev->lock);
  5681. }
  5682. static
  5683. int addrconf_disable_policy(const struct ctl_table *ctl, int *valp, int val)
  5684. {
  5685. struct net *net = (struct net *)ctl->extra2;
  5686. struct inet6_dev *idev;
  5687. if (valp == &net->ipv6.devconf_dflt->disable_policy) {
  5688. WRITE_ONCE(*valp, val);
  5689. return 0;
  5690. }
  5691. if (!rtnl_trylock())
  5692. return restart_syscall();
  5693. WRITE_ONCE(*valp, val);
  5694. if (valp == &net->ipv6.devconf_all->disable_policy) {
  5695. struct net_device *dev;
  5696. for_each_netdev(net, dev) {
  5697. idev = __in6_dev_get(dev);
  5698. if (idev)
  5699. addrconf_disable_policy_idev(idev, val);
  5700. }
  5701. } else {
  5702. idev = (struct inet6_dev *)ctl->extra1;
  5703. addrconf_disable_policy_idev(idev, val);
  5704. }
  5705. rtnl_unlock();
  5706. return 0;
  5707. }
  5708. static int addrconf_sysctl_disable_policy(const struct ctl_table *ctl, int write,
  5709. void *buffer, size_t *lenp, loff_t *ppos)
  5710. {
  5711. int *valp = ctl->data;
  5712. int val = *valp;
  5713. loff_t pos = *ppos;
  5714. struct ctl_table lctl;
  5715. int ret;
  5716. lctl = *ctl;
  5717. lctl.data = &val;
  5718. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5719. if (write && (*valp != val))
  5720. ret = addrconf_disable_policy(ctl, valp, val);
  5721. if (ret)
  5722. *ppos = pos;
  5723. return ret;
  5724. }
  5725. static int minus_one = -1;
  5726. static const int two_five_five = 255;
  5727. static u32 ioam6_if_id_max = U16_MAX;
  5728. static const struct ctl_table addrconf_sysctl[] = {
  5729. {
  5730. .procname = "forwarding",
  5731. .data = &ipv6_devconf.forwarding,
  5732. .maxlen = sizeof(int),
  5733. .mode = 0644,
  5734. .proc_handler = addrconf_sysctl_forward,
  5735. },
  5736. {
  5737. .procname = "hop_limit",
  5738. .data = &ipv6_devconf.hop_limit,
  5739. .maxlen = sizeof(int),
  5740. .mode = 0644,
  5741. .proc_handler = proc_dointvec_minmax,
  5742. .extra1 = (void *)SYSCTL_ONE,
  5743. .extra2 = (void *)&two_five_five,
  5744. },
  5745. {
  5746. .procname = "mtu",
  5747. .data = &ipv6_devconf.mtu6,
  5748. .maxlen = sizeof(int),
  5749. .mode = 0644,
  5750. .proc_handler = addrconf_sysctl_mtu,
  5751. },
  5752. {
  5753. .procname = "accept_ra",
  5754. .data = &ipv6_devconf.accept_ra,
  5755. .maxlen = sizeof(int),
  5756. .mode = 0644,
  5757. .proc_handler = proc_dointvec,
  5758. },
  5759. {
  5760. .procname = "accept_redirects",
  5761. .data = &ipv6_devconf.accept_redirects,
  5762. .maxlen = sizeof(int),
  5763. .mode = 0644,
  5764. .proc_handler = proc_dointvec,
  5765. },
  5766. {
  5767. .procname = "autoconf",
  5768. .data = &ipv6_devconf.autoconf,
  5769. .maxlen = sizeof(int),
  5770. .mode = 0644,
  5771. .proc_handler = proc_dointvec,
  5772. },
  5773. {
  5774. .procname = "dad_transmits",
  5775. .data = &ipv6_devconf.dad_transmits,
  5776. .maxlen = sizeof(int),
  5777. .mode = 0644,
  5778. .proc_handler = proc_dointvec,
  5779. },
  5780. {
  5781. .procname = "router_solicitations",
  5782. .data = &ipv6_devconf.rtr_solicits,
  5783. .maxlen = sizeof(int),
  5784. .mode = 0644,
  5785. .proc_handler = proc_dointvec_minmax,
  5786. .extra1 = &minus_one,
  5787. },
  5788. {
  5789. .procname = "router_solicitation_interval",
  5790. .data = &ipv6_devconf.rtr_solicit_interval,
  5791. .maxlen = sizeof(int),
  5792. .mode = 0644,
  5793. .proc_handler = proc_dointvec_jiffies,
  5794. },
  5795. {
  5796. .procname = "router_solicitation_max_interval",
  5797. .data = &ipv6_devconf.rtr_solicit_max_interval,
  5798. .maxlen = sizeof(int),
  5799. .mode = 0644,
  5800. .proc_handler = proc_dointvec_jiffies,
  5801. },
  5802. {
  5803. .procname = "router_solicitation_delay",
  5804. .data = &ipv6_devconf.rtr_solicit_delay,
  5805. .maxlen = sizeof(int),
  5806. .mode = 0644,
  5807. .proc_handler = proc_dointvec_jiffies,
  5808. },
  5809. {
  5810. .procname = "force_mld_version",
  5811. .data = &ipv6_devconf.force_mld_version,
  5812. .maxlen = sizeof(int),
  5813. .mode = 0644,
  5814. .proc_handler = proc_dointvec,
  5815. },
  5816. {
  5817. .procname = "mldv1_unsolicited_report_interval",
  5818. .data =
  5819. &ipv6_devconf.mldv1_unsolicited_report_interval,
  5820. .maxlen = sizeof(int),
  5821. .mode = 0644,
  5822. .proc_handler = proc_dointvec_ms_jiffies,
  5823. },
  5824. {
  5825. .procname = "mldv2_unsolicited_report_interval",
  5826. .data =
  5827. &ipv6_devconf.mldv2_unsolicited_report_interval,
  5828. .maxlen = sizeof(int),
  5829. .mode = 0644,
  5830. .proc_handler = proc_dointvec_ms_jiffies,
  5831. },
  5832. {
  5833. .procname = "use_tempaddr",
  5834. .data = &ipv6_devconf.use_tempaddr,
  5835. .maxlen = sizeof(int),
  5836. .mode = 0644,
  5837. .proc_handler = proc_dointvec,
  5838. },
  5839. {
  5840. .procname = "temp_valid_lft",
  5841. .data = &ipv6_devconf.temp_valid_lft,
  5842. .maxlen = sizeof(int),
  5843. .mode = 0644,
  5844. .proc_handler = proc_dointvec,
  5845. },
  5846. {
  5847. .procname = "temp_prefered_lft",
  5848. .data = &ipv6_devconf.temp_prefered_lft,
  5849. .maxlen = sizeof(int),
  5850. .mode = 0644,
  5851. .proc_handler = proc_dointvec,
  5852. },
  5853. {
  5854. .procname = "regen_min_advance",
  5855. .data = &ipv6_devconf.regen_min_advance,
  5856. .maxlen = sizeof(int),
  5857. .mode = 0644,
  5858. .proc_handler = proc_dointvec,
  5859. },
  5860. {
  5861. .procname = "regen_max_retry",
  5862. .data = &ipv6_devconf.regen_max_retry,
  5863. .maxlen = sizeof(int),
  5864. .mode = 0644,
  5865. .proc_handler = proc_dointvec,
  5866. },
  5867. {
  5868. .procname = "max_desync_factor",
  5869. .data = &ipv6_devconf.max_desync_factor,
  5870. .maxlen = sizeof(int),
  5871. .mode = 0644,
  5872. .proc_handler = proc_dointvec,
  5873. },
  5874. {
  5875. .procname = "max_addresses",
  5876. .data = &ipv6_devconf.max_addresses,
  5877. .maxlen = sizeof(int),
  5878. .mode = 0644,
  5879. .proc_handler = proc_dointvec,
  5880. },
  5881. {
  5882. .procname = "accept_ra_defrtr",
  5883. .data = &ipv6_devconf.accept_ra_defrtr,
  5884. .maxlen = sizeof(int),
  5885. .mode = 0644,
  5886. .proc_handler = proc_dointvec,
  5887. },
  5888. {
  5889. .procname = "ra_defrtr_metric",
  5890. .data = &ipv6_devconf.ra_defrtr_metric,
  5891. .maxlen = sizeof(u32),
  5892. .mode = 0644,
  5893. .proc_handler = proc_douintvec_minmax,
  5894. .extra1 = (void *)SYSCTL_ONE,
  5895. },
  5896. {
  5897. .procname = "accept_ra_min_hop_limit",
  5898. .data = &ipv6_devconf.accept_ra_min_hop_limit,
  5899. .maxlen = sizeof(int),
  5900. .mode = 0644,
  5901. .proc_handler = proc_dointvec,
  5902. },
  5903. {
  5904. .procname = "accept_ra_min_lft",
  5905. .data = &ipv6_devconf.accept_ra_min_lft,
  5906. .maxlen = sizeof(int),
  5907. .mode = 0644,
  5908. .proc_handler = proc_dointvec,
  5909. },
  5910. {
  5911. .procname = "accept_ra_pinfo",
  5912. .data = &ipv6_devconf.accept_ra_pinfo,
  5913. .maxlen = sizeof(int),
  5914. .mode = 0644,
  5915. .proc_handler = proc_dointvec,
  5916. },
  5917. {
  5918. .procname = "ra_honor_pio_life",
  5919. .data = &ipv6_devconf.ra_honor_pio_life,
  5920. .maxlen = sizeof(u8),
  5921. .mode = 0644,
  5922. .proc_handler = proc_dou8vec_minmax,
  5923. .extra1 = SYSCTL_ZERO,
  5924. .extra2 = SYSCTL_ONE,
  5925. },
  5926. {
  5927. .procname = "ra_honor_pio_pflag",
  5928. .data = &ipv6_devconf.ra_honor_pio_pflag,
  5929. .maxlen = sizeof(u8),
  5930. .mode = 0644,
  5931. .proc_handler = proc_dou8vec_minmax,
  5932. .extra1 = SYSCTL_ZERO,
  5933. .extra2 = SYSCTL_ONE,
  5934. },
  5935. #ifdef CONFIG_IPV6_ROUTER_PREF
  5936. {
  5937. .procname = "accept_ra_rtr_pref",
  5938. .data = &ipv6_devconf.accept_ra_rtr_pref,
  5939. .maxlen = sizeof(int),
  5940. .mode = 0644,
  5941. .proc_handler = proc_dointvec,
  5942. },
  5943. {
  5944. .procname = "router_probe_interval",
  5945. .data = &ipv6_devconf.rtr_probe_interval,
  5946. .maxlen = sizeof(int),
  5947. .mode = 0644,
  5948. .proc_handler = proc_dointvec_jiffies,
  5949. },
  5950. #ifdef CONFIG_IPV6_ROUTE_INFO
  5951. {
  5952. .procname = "accept_ra_rt_info_min_plen",
  5953. .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
  5954. .maxlen = sizeof(int),
  5955. .mode = 0644,
  5956. .proc_handler = proc_dointvec,
  5957. },
  5958. {
  5959. .procname = "accept_ra_rt_info_max_plen",
  5960. .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
  5961. .maxlen = sizeof(int),
  5962. .mode = 0644,
  5963. .proc_handler = proc_dointvec,
  5964. },
  5965. #endif
  5966. #endif
  5967. {
  5968. .procname = "proxy_ndp",
  5969. .data = &ipv6_devconf.proxy_ndp,
  5970. .maxlen = sizeof(int),
  5971. .mode = 0644,
  5972. .proc_handler = addrconf_sysctl_proxy_ndp,
  5973. },
  5974. {
  5975. .procname = "accept_source_route",
  5976. .data = &ipv6_devconf.accept_source_route,
  5977. .maxlen = sizeof(int),
  5978. .mode = 0644,
  5979. .proc_handler = proc_dointvec,
  5980. },
  5981. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  5982. {
  5983. .procname = "optimistic_dad",
  5984. .data = &ipv6_devconf.optimistic_dad,
  5985. .maxlen = sizeof(int),
  5986. .mode = 0644,
  5987. .proc_handler = proc_dointvec,
  5988. },
  5989. {
  5990. .procname = "use_optimistic",
  5991. .data = &ipv6_devconf.use_optimistic,
  5992. .maxlen = sizeof(int),
  5993. .mode = 0644,
  5994. .proc_handler = proc_dointvec,
  5995. },
  5996. #endif
  5997. #ifdef CONFIG_IPV6_MROUTE
  5998. {
  5999. .procname = "mc_forwarding",
  6000. .data = &ipv6_devconf.mc_forwarding,
  6001. .maxlen = sizeof(int),
  6002. .mode = 0444,
  6003. .proc_handler = proc_dointvec,
  6004. },
  6005. #endif
  6006. {
  6007. .procname = "disable_ipv6",
  6008. .data = &ipv6_devconf.disable_ipv6,
  6009. .maxlen = sizeof(int),
  6010. .mode = 0644,
  6011. .proc_handler = addrconf_sysctl_disable,
  6012. },
  6013. {
  6014. .procname = "accept_dad",
  6015. .data = &ipv6_devconf.accept_dad,
  6016. .maxlen = sizeof(int),
  6017. .mode = 0644,
  6018. .proc_handler = proc_dointvec,
  6019. },
  6020. {
  6021. .procname = "force_tllao",
  6022. .data = &ipv6_devconf.force_tllao,
  6023. .maxlen = sizeof(int),
  6024. .mode = 0644,
  6025. .proc_handler = proc_dointvec
  6026. },
  6027. {
  6028. .procname = "ndisc_notify",
  6029. .data = &ipv6_devconf.ndisc_notify,
  6030. .maxlen = sizeof(int),
  6031. .mode = 0644,
  6032. .proc_handler = proc_dointvec
  6033. },
  6034. {
  6035. .procname = "suppress_frag_ndisc",
  6036. .data = &ipv6_devconf.suppress_frag_ndisc,
  6037. .maxlen = sizeof(int),
  6038. .mode = 0644,
  6039. .proc_handler = proc_dointvec
  6040. },
  6041. {
  6042. .procname = "accept_ra_from_local",
  6043. .data = &ipv6_devconf.accept_ra_from_local,
  6044. .maxlen = sizeof(int),
  6045. .mode = 0644,
  6046. .proc_handler = proc_dointvec,
  6047. },
  6048. {
  6049. .procname = "accept_ra_mtu",
  6050. .data = &ipv6_devconf.accept_ra_mtu,
  6051. .maxlen = sizeof(int),
  6052. .mode = 0644,
  6053. .proc_handler = proc_dointvec,
  6054. },
  6055. {
  6056. .procname = "stable_secret",
  6057. .data = &ipv6_devconf.stable_secret,
  6058. .maxlen = IPV6_MAX_STRLEN,
  6059. .mode = 0600,
  6060. .proc_handler = addrconf_sysctl_stable_secret,
  6061. },
  6062. {
  6063. .procname = "use_oif_addrs_only",
  6064. .data = &ipv6_devconf.use_oif_addrs_only,
  6065. .maxlen = sizeof(int),
  6066. .mode = 0644,
  6067. .proc_handler = proc_dointvec,
  6068. },
  6069. {
  6070. .procname = "ignore_routes_with_linkdown",
  6071. .data = &ipv6_devconf.ignore_routes_with_linkdown,
  6072. .maxlen = sizeof(int),
  6073. .mode = 0644,
  6074. .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
  6075. },
  6076. {
  6077. .procname = "drop_unicast_in_l2_multicast",
  6078. .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
  6079. .maxlen = sizeof(int),
  6080. .mode = 0644,
  6081. .proc_handler = proc_dointvec,
  6082. },
  6083. {
  6084. .procname = "drop_unsolicited_na",
  6085. .data = &ipv6_devconf.drop_unsolicited_na,
  6086. .maxlen = sizeof(int),
  6087. .mode = 0644,
  6088. .proc_handler = proc_dointvec,
  6089. },
  6090. {
  6091. .procname = "keep_addr_on_down",
  6092. .data = &ipv6_devconf.keep_addr_on_down,
  6093. .maxlen = sizeof(int),
  6094. .mode = 0644,
  6095. .proc_handler = proc_dointvec,
  6096. },
  6097. {
  6098. .procname = "seg6_enabled",
  6099. .data = &ipv6_devconf.seg6_enabled,
  6100. .maxlen = sizeof(int),
  6101. .mode = 0644,
  6102. .proc_handler = proc_dointvec,
  6103. },
  6104. #ifdef CONFIG_IPV6_SEG6_HMAC
  6105. {
  6106. .procname = "seg6_require_hmac",
  6107. .data = &ipv6_devconf.seg6_require_hmac,
  6108. .maxlen = sizeof(int),
  6109. .mode = 0644,
  6110. .proc_handler = proc_dointvec,
  6111. },
  6112. #endif
  6113. {
  6114. .procname = "enhanced_dad",
  6115. .data = &ipv6_devconf.enhanced_dad,
  6116. .maxlen = sizeof(int),
  6117. .mode = 0644,
  6118. .proc_handler = proc_dointvec,
  6119. },
  6120. {
  6121. .procname = "addr_gen_mode",
  6122. .data = &ipv6_devconf.addr_gen_mode,
  6123. .maxlen = sizeof(int),
  6124. .mode = 0644,
  6125. .proc_handler = addrconf_sysctl_addr_gen_mode,
  6126. },
  6127. {
  6128. .procname = "disable_policy",
  6129. .data = &ipv6_devconf.disable_policy,
  6130. .maxlen = sizeof(int),
  6131. .mode = 0644,
  6132. .proc_handler = addrconf_sysctl_disable_policy,
  6133. },
  6134. {
  6135. .procname = "ndisc_tclass",
  6136. .data = &ipv6_devconf.ndisc_tclass,
  6137. .maxlen = sizeof(int),
  6138. .mode = 0644,
  6139. .proc_handler = proc_dointvec_minmax,
  6140. .extra1 = (void *)SYSCTL_ZERO,
  6141. .extra2 = (void *)&two_five_five,
  6142. },
  6143. {
  6144. .procname = "rpl_seg_enabled",
  6145. .data = &ipv6_devconf.rpl_seg_enabled,
  6146. .maxlen = sizeof(int),
  6147. .mode = 0644,
  6148. .proc_handler = proc_dointvec,
  6149. },
  6150. {
  6151. .procname = "ioam6_enabled",
  6152. .data = &ipv6_devconf.ioam6_enabled,
  6153. .maxlen = sizeof(u8),
  6154. .mode = 0644,
  6155. .proc_handler = proc_dou8vec_minmax,
  6156. .extra1 = (void *)SYSCTL_ZERO,
  6157. .extra2 = (void *)SYSCTL_ONE,
  6158. },
  6159. {
  6160. .procname = "ioam6_id",
  6161. .data = &ipv6_devconf.ioam6_id,
  6162. .maxlen = sizeof(u32),
  6163. .mode = 0644,
  6164. .proc_handler = proc_douintvec_minmax,
  6165. .extra1 = (void *)SYSCTL_ZERO,
  6166. .extra2 = (void *)&ioam6_if_id_max,
  6167. },
  6168. {
  6169. .procname = "ioam6_id_wide",
  6170. .data = &ipv6_devconf.ioam6_id_wide,
  6171. .maxlen = sizeof(u32),
  6172. .mode = 0644,
  6173. .proc_handler = proc_douintvec,
  6174. },
  6175. {
  6176. .procname = "ndisc_evict_nocarrier",
  6177. .data = &ipv6_devconf.ndisc_evict_nocarrier,
  6178. .maxlen = sizeof(u8),
  6179. .mode = 0644,
  6180. .proc_handler = proc_dou8vec_minmax,
  6181. .extra1 = (void *)SYSCTL_ZERO,
  6182. .extra2 = (void *)SYSCTL_ONE,
  6183. },
  6184. {
  6185. .procname = "accept_untracked_na",
  6186. .data = &ipv6_devconf.accept_untracked_na,
  6187. .maxlen = sizeof(int),
  6188. .mode = 0644,
  6189. .proc_handler = proc_dointvec_minmax,
  6190. .extra1 = SYSCTL_ZERO,
  6191. .extra2 = SYSCTL_TWO,
  6192. },
  6193. };
  6194. static int __addrconf_sysctl_register(struct net *net, char *dev_name,
  6195. struct inet6_dev *idev, struct ipv6_devconf *p)
  6196. {
  6197. size_t table_size = ARRAY_SIZE(addrconf_sysctl);
  6198. int i, ifindex;
  6199. struct ctl_table *table;
  6200. char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
  6201. table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
  6202. if (!table)
  6203. goto out;
  6204. for (i = 0; i < table_size; i++) {
  6205. table[i].data += (char *)p - (char *)&ipv6_devconf;
  6206. /* If one of these is already set, then it is not safe to
  6207. * overwrite either of them: this makes proc_dointvec_minmax
  6208. * usable.
  6209. */
  6210. if (!table[i].extra1 && !table[i].extra2) {
  6211. table[i].extra1 = idev; /* embedded; no ref */
  6212. table[i].extra2 = net;
  6213. }
  6214. }
  6215. snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
  6216. p->sysctl_header = register_net_sysctl_sz(net, path, table,
  6217. table_size);
  6218. if (!p->sysctl_header)
  6219. goto free;
  6220. if (!strcmp(dev_name, "all"))
  6221. ifindex = NETCONFA_IFINDEX_ALL;
  6222. else if (!strcmp(dev_name, "default"))
  6223. ifindex = NETCONFA_IFINDEX_DEFAULT;
  6224. else
  6225. ifindex = idev->dev->ifindex;
  6226. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
  6227. ifindex, p);
  6228. return 0;
  6229. free:
  6230. kfree(table);
  6231. out:
  6232. return -ENOBUFS;
  6233. }
  6234. static void __addrconf_sysctl_unregister(struct net *net,
  6235. struct ipv6_devconf *p, int ifindex)
  6236. {
  6237. const struct ctl_table *table;
  6238. if (!p->sysctl_header)
  6239. return;
  6240. table = p->sysctl_header->ctl_table_arg;
  6241. unregister_net_sysctl_table(p->sysctl_header);
  6242. p->sysctl_header = NULL;
  6243. kfree(table);
  6244. inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
  6245. }
  6246. static int addrconf_sysctl_register(struct inet6_dev *idev)
  6247. {
  6248. int err;
  6249. if (!sysctl_dev_name_is_allowed(idev->dev->name))
  6250. return -EINVAL;
  6251. err = neigh_sysctl_register(idev->dev, idev->nd_parms,
  6252. &ndisc_ifinfo_sysctl_change);
  6253. if (err)
  6254. return err;
  6255. err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
  6256. idev, &idev->cnf);
  6257. if (err)
  6258. neigh_sysctl_unregister(idev->nd_parms);
  6259. return err;
  6260. }
  6261. static void addrconf_sysctl_unregister(struct inet6_dev *idev)
  6262. {
  6263. __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
  6264. idev->dev->ifindex);
  6265. neigh_sysctl_unregister(idev->nd_parms);
  6266. }
  6267. #endif
  6268. static int __net_init addrconf_init_net(struct net *net)
  6269. {
  6270. int err = -ENOMEM;
  6271. struct ipv6_devconf *all, *dflt;
  6272. spin_lock_init(&net->ipv6.addrconf_hash_lock);
  6273. INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
  6274. net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
  6275. sizeof(struct hlist_head),
  6276. GFP_KERNEL);
  6277. if (!net->ipv6.inet6_addr_lst)
  6278. goto err_alloc_addr;
  6279. all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
  6280. if (!all)
  6281. goto err_alloc_all;
  6282. dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
  6283. if (!dflt)
  6284. goto err_alloc_dflt;
  6285. if (!net_eq(net, &init_net)) {
  6286. switch (net_inherit_devconf()) {
  6287. case 1: /* copy from init_net */
  6288. memcpy(all, init_net.ipv6.devconf_all,
  6289. sizeof(ipv6_devconf));
  6290. memcpy(dflt, init_net.ipv6.devconf_dflt,
  6291. sizeof(ipv6_devconf_dflt));
  6292. break;
  6293. case 3: /* copy from the current netns */
  6294. memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
  6295. sizeof(ipv6_devconf));
  6296. memcpy(dflt,
  6297. current->nsproxy->net_ns->ipv6.devconf_dflt,
  6298. sizeof(ipv6_devconf_dflt));
  6299. break;
  6300. case 0:
  6301. case 2:
  6302. /* use compiled values */
  6303. break;
  6304. }
  6305. }
  6306. /* these will be inherited by all namespaces */
  6307. dflt->autoconf = ipv6_defaults.autoconf;
  6308. dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
  6309. dflt->stable_secret.initialized = false;
  6310. all->stable_secret.initialized = false;
  6311. net->ipv6.devconf_all = all;
  6312. net->ipv6.devconf_dflt = dflt;
  6313. #ifdef CONFIG_SYSCTL
  6314. err = __addrconf_sysctl_register(net, "all", NULL, all);
  6315. if (err < 0)
  6316. goto err_reg_all;
  6317. err = __addrconf_sysctl_register(net, "default", NULL, dflt);
  6318. if (err < 0)
  6319. goto err_reg_dflt;
  6320. #endif
  6321. return 0;
  6322. #ifdef CONFIG_SYSCTL
  6323. err_reg_dflt:
  6324. __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
  6325. err_reg_all:
  6326. kfree(dflt);
  6327. net->ipv6.devconf_dflt = NULL;
  6328. #endif
  6329. err_alloc_dflt:
  6330. kfree(all);
  6331. net->ipv6.devconf_all = NULL;
  6332. err_alloc_all:
  6333. kfree(net->ipv6.inet6_addr_lst);
  6334. err_alloc_addr:
  6335. return err;
  6336. }
  6337. static void __net_exit addrconf_exit_net(struct net *net)
  6338. {
  6339. int i;
  6340. #ifdef CONFIG_SYSCTL
  6341. __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
  6342. NETCONFA_IFINDEX_DEFAULT);
  6343. __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
  6344. NETCONFA_IFINDEX_ALL);
  6345. #endif
  6346. kfree(net->ipv6.devconf_dflt);
  6347. net->ipv6.devconf_dflt = NULL;
  6348. kfree(net->ipv6.devconf_all);
  6349. net->ipv6.devconf_all = NULL;
  6350. cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
  6351. /*
  6352. * Check hash table, then free it.
  6353. */
  6354. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  6355. WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
  6356. kfree(net->ipv6.inet6_addr_lst);
  6357. net->ipv6.inet6_addr_lst = NULL;
  6358. }
  6359. static struct pernet_operations addrconf_ops = {
  6360. .init = addrconf_init_net,
  6361. .exit = addrconf_exit_net,
  6362. };
  6363. static struct rtnl_af_ops inet6_ops __read_mostly = {
  6364. .family = AF_INET6,
  6365. .fill_link_af = inet6_fill_link_af,
  6366. .get_link_af_size = inet6_get_link_af_size,
  6367. .validate_link_af = inet6_validate_link_af,
  6368. .set_link_af = inet6_set_link_af,
  6369. };
  6370. /*
  6371. * Init / cleanup code
  6372. */
  6373. int __init addrconf_init(void)
  6374. {
  6375. struct inet6_dev *idev;
  6376. int err;
  6377. err = ipv6_addr_label_init();
  6378. if (err < 0) {
  6379. pr_crit("%s: cannot initialize default policy table: %d\n",
  6380. __func__, err);
  6381. goto out;
  6382. }
  6383. err = register_pernet_subsys(&addrconf_ops);
  6384. if (err < 0)
  6385. goto out_addrlabel;
  6386. /* All works using addrconf_wq need to lock rtnl. */
  6387. addrconf_wq = create_singlethread_workqueue("ipv6_addrconf");
  6388. if (!addrconf_wq) {
  6389. err = -ENOMEM;
  6390. goto out_nowq;
  6391. }
  6392. rtnl_lock();
  6393. idev = ipv6_add_dev(blackhole_netdev);
  6394. rtnl_unlock();
  6395. if (IS_ERR(idev)) {
  6396. err = PTR_ERR(idev);
  6397. goto errlo;
  6398. }
  6399. ip6_route_init_special_entries();
  6400. register_netdevice_notifier(&ipv6_dev_notf);
  6401. addrconf_verify(&init_net);
  6402. rtnl_af_register(&inet6_ops);
  6403. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
  6404. NULL, inet6_dump_ifinfo, RTNL_FLAG_DUMP_UNLOCKED);
  6405. if (err < 0)
  6406. goto errout;
  6407. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
  6408. inet6_rtm_newaddr, NULL, 0);
  6409. if (err < 0)
  6410. goto errout;
  6411. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
  6412. inet6_rtm_deladdr, NULL, 0);
  6413. if (err < 0)
  6414. goto errout;
  6415. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
  6416. inet6_rtm_getaddr, inet6_dump_ifaddr,
  6417. RTNL_FLAG_DOIT_UNLOCKED |
  6418. RTNL_FLAG_DUMP_UNLOCKED);
  6419. if (err < 0)
  6420. goto errout;
  6421. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
  6422. NULL, inet6_dump_ifmcaddr,
  6423. RTNL_FLAG_DUMP_UNLOCKED);
  6424. if (err < 0)
  6425. goto errout;
  6426. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
  6427. NULL, inet6_dump_ifacaddr,
  6428. RTNL_FLAG_DUMP_UNLOCKED);
  6429. if (err < 0)
  6430. goto errout;
  6431. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
  6432. inet6_netconf_get_devconf,
  6433. inet6_netconf_dump_devconf,
  6434. RTNL_FLAG_DOIT_UNLOCKED |
  6435. RTNL_FLAG_DUMP_UNLOCKED);
  6436. if (err < 0)
  6437. goto errout;
  6438. err = ipv6_addr_label_rtnl_register();
  6439. if (err < 0)
  6440. goto errout;
  6441. return 0;
  6442. errout:
  6443. rtnl_unregister_all(PF_INET6);
  6444. rtnl_af_unregister(&inet6_ops);
  6445. unregister_netdevice_notifier(&ipv6_dev_notf);
  6446. errlo:
  6447. destroy_workqueue(addrconf_wq);
  6448. out_nowq:
  6449. unregister_pernet_subsys(&addrconf_ops);
  6450. out_addrlabel:
  6451. ipv6_addr_label_cleanup();
  6452. out:
  6453. return err;
  6454. }
  6455. void addrconf_cleanup(void)
  6456. {
  6457. struct net_device *dev;
  6458. unregister_netdevice_notifier(&ipv6_dev_notf);
  6459. unregister_pernet_subsys(&addrconf_ops);
  6460. ipv6_addr_label_cleanup();
  6461. rtnl_af_unregister(&inet6_ops);
  6462. rtnl_lock();
  6463. /* clean dev list */
  6464. for_each_netdev(&init_net, dev) {
  6465. if (__in6_dev_get(dev) == NULL)
  6466. continue;
  6467. addrconf_ifdown(dev, true);
  6468. }
  6469. addrconf_ifdown(init_net.loopback_dev, true);
  6470. rtnl_unlock();
  6471. destroy_workqueue(addrconf_wq);
  6472. }