addrconf.c 188 KB

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