addrconf.c 166 KB

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