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_UNICAST,
  2073. .fc_nlinfo.nl_net = dev_net(dev),
  2074. };
  2075. ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
  2076. ip6_route_add(&cfg, GFP_KERNEL, NULL);
  2077. }
  2078. static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
  2079. {
  2080. struct inet6_dev *idev;
  2081. ASSERT_RTNL();
  2082. idev = ipv6_find_idev(dev);
  2083. if (!idev)
  2084. return ERR_PTR(-ENOBUFS);
  2085. if (idev->cnf.disable_ipv6)
  2086. return ERR_PTR(-EACCES);
  2087. /* Add default multicast route */
  2088. if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
  2089. addrconf_add_mroute(dev);
  2090. return idev;
  2091. }
  2092. static void manage_tempaddrs(struct inet6_dev *idev,
  2093. struct inet6_ifaddr *ifp,
  2094. __u32 valid_lft, __u32 prefered_lft,
  2095. bool create, unsigned long now)
  2096. {
  2097. u32 flags;
  2098. struct inet6_ifaddr *ift;
  2099. read_lock_bh(&idev->lock);
  2100. /* update all temporary addresses in the list */
  2101. list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
  2102. int age, max_valid, max_prefered;
  2103. if (ifp != ift->ifpub)
  2104. continue;
  2105. /* RFC 4941 section 3.3:
  2106. * If a received option will extend the lifetime of a public
  2107. * address, the lifetimes of temporary addresses should
  2108. * be extended, subject to the overall constraint that no
  2109. * temporary addresses should ever remain "valid" or "preferred"
  2110. * for a time longer than (TEMP_VALID_LIFETIME) or
  2111. * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
  2112. */
  2113. age = (now - ift->cstamp) / HZ;
  2114. max_valid = idev->cnf.temp_valid_lft - age;
  2115. if (max_valid < 0)
  2116. max_valid = 0;
  2117. max_prefered = idev->cnf.temp_prefered_lft -
  2118. idev->desync_factor - age;
  2119. if (max_prefered < 0)
  2120. max_prefered = 0;
  2121. if (valid_lft > max_valid)
  2122. valid_lft = max_valid;
  2123. if (prefered_lft > max_prefered)
  2124. prefered_lft = max_prefered;
  2125. spin_lock(&ift->lock);
  2126. flags = ift->flags;
  2127. ift->valid_lft = valid_lft;
  2128. ift->prefered_lft = prefered_lft;
  2129. ift->tstamp = now;
  2130. if (prefered_lft > 0)
  2131. ift->flags &= ~IFA_F_DEPRECATED;
  2132. spin_unlock(&ift->lock);
  2133. if (!(flags&IFA_F_TENTATIVE))
  2134. ipv6_ifa_notify(0, ift);
  2135. }
  2136. if ((create || list_empty(&idev->tempaddr_list)) &&
  2137. idev->cnf.use_tempaddr > 0) {
  2138. /* When a new public address is created as described
  2139. * in [ADDRCONF], also create a new temporary address.
  2140. * Also create a temporary address if it's enabled but
  2141. * no temporary address currently exists.
  2142. */
  2143. read_unlock_bh(&idev->lock);
  2144. ipv6_create_tempaddr(ifp, NULL, false);
  2145. } else {
  2146. read_unlock_bh(&idev->lock);
  2147. }
  2148. }
  2149. static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
  2150. {
  2151. return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
  2152. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
  2153. }
  2154. int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
  2155. const struct prefix_info *pinfo,
  2156. struct inet6_dev *in6_dev,
  2157. const struct in6_addr *addr, int addr_type,
  2158. u32 addr_flags, bool sllao, bool tokenized,
  2159. __u32 valid_lft, u32 prefered_lft)
  2160. {
  2161. struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
  2162. int create = 0, update_lft = 0;
  2163. if (!ifp && valid_lft) {
  2164. int max_addresses = in6_dev->cnf.max_addresses;
  2165. struct ifa6_config cfg = {
  2166. .pfx = addr,
  2167. .plen = pinfo->prefix_len,
  2168. .ifa_flags = addr_flags,
  2169. .valid_lft = valid_lft,
  2170. .preferred_lft = prefered_lft,
  2171. .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
  2172. };
  2173. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2174. if ((net->ipv6.devconf_all->optimistic_dad ||
  2175. in6_dev->cnf.optimistic_dad) &&
  2176. !net->ipv6.devconf_all->forwarding && sllao)
  2177. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  2178. #endif
  2179. /* Do not allow to create too much of autoconfigured
  2180. * addresses; this would be too easy way to crash kernel.
  2181. */
  2182. if (!max_addresses ||
  2183. ipv6_count_addresses(in6_dev) < max_addresses)
  2184. ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
  2185. if (IS_ERR_OR_NULL(ifp))
  2186. return -1;
  2187. create = 1;
  2188. spin_lock_bh(&ifp->lock);
  2189. ifp->flags |= IFA_F_MANAGETEMPADDR;
  2190. ifp->cstamp = jiffies;
  2191. ifp->tokenized = tokenized;
  2192. spin_unlock_bh(&ifp->lock);
  2193. addrconf_dad_start(ifp);
  2194. }
  2195. if (ifp) {
  2196. u32 flags;
  2197. unsigned long now;
  2198. u32 stored_lft;
  2199. /* update lifetime (RFC2462 5.5.3 e) */
  2200. spin_lock_bh(&ifp->lock);
  2201. now = jiffies;
  2202. if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
  2203. stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
  2204. else
  2205. stored_lft = 0;
  2206. if (!create && stored_lft) {
  2207. const u32 minimum_lft = min_t(u32,
  2208. stored_lft, MIN_VALID_LIFETIME);
  2209. valid_lft = max(valid_lft, minimum_lft);
  2210. /* RFC4862 Section 5.5.3e:
  2211. * "Note that the preferred lifetime of the
  2212. * corresponding address is always reset to
  2213. * the Preferred Lifetime in the received
  2214. * Prefix Information option, regardless of
  2215. * whether the valid lifetime is also reset or
  2216. * ignored."
  2217. *
  2218. * So we should always update prefered_lft here.
  2219. */
  2220. update_lft = 1;
  2221. }
  2222. if (update_lft) {
  2223. ifp->valid_lft = valid_lft;
  2224. ifp->prefered_lft = prefered_lft;
  2225. ifp->tstamp = now;
  2226. flags = ifp->flags;
  2227. ifp->flags &= ~IFA_F_DEPRECATED;
  2228. spin_unlock_bh(&ifp->lock);
  2229. if (!(flags&IFA_F_TENTATIVE))
  2230. ipv6_ifa_notify(0, ifp);
  2231. } else
  2232. spin_unlock_bh(&ifp->lock);
  2233. manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
  2234. create, now);
  2235. in6_ifa_put(ifp);
  2236. addrconf_verify();
  2237. }
  2238. return 0;
  2239. }
  2240. EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
  2241. void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
  2242. {
  2243. struct prefix_info *pinfo;
  2244. __u32 valid_lft;
  2245. __u32 prefered_lft;
  2246. int addr_type, err;
  2247. u32 addr_flags = 0;
  2248. struct inet6_dev *in6_dev;
  2249. struct net *net = dev_net(dev);
  2250. pinfo = (struct prefix_info *) opt;
  2251. if (len < sizeof(struct prefix_info)) {
  2252. netdev_dbg(dev, "addrconf: prefix option too short\n");
  2253. return;
  2254. }
  2255. /*
  2256. * Validation checks ([ADDRCONF], page 19)
  2257. */
  2258. addr_type = ipv6_addr_type(&pinfo->prefix);
  2259. if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
  2260. return;
  2261. valid_lft = ntohl(pinfo->valid);
  2262. prefered_lft = ntohl(pinfo->prefered);
  2263. if (prefered_lft > valid_lft) {
  2264. net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
  2265. return;
  2266. }
  2267. in6_dev = in6_dev_get(dev);
  2268. if (!in6_dev) {
  2269. net_dbg_ratelimited("addrconf: device %s not configured\n",
  2270. dev->name);
  2271. return;
  2272. }
  2273. /*
  2274. * Two things going on here:
  2275. * 1) Add routes for on-link prefixes
  2276. * 2) Configure prefixes with the auto flag set
  2277. */
  2278. if (pinfo->onlink) {
  2279. struct fib6_info *rt;
  2280. unsigned long rt_expires;
  2281. /* Avoid arithmetic overflow. Really, we could
  2282. * save rt_expires in seconds, likely valid_lft,
  2283. * but it would require division in fib gc, that it
  2284. * not good.
  2285. */
  2286. if (HZ > USER_HZ)
  2287. rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
  2288. else
  2289. rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
  2290. if (addrconf_finite_timeout(rt_expires))
  2291. rt_expires *= HZ;
  2292. rt = addrconf_get_prefix_route(&pinfo->prefix,
  2293. pinfo->prefix_len,
  2294. dev,
  2295. RTF_ADDRCONF | RTF_PREFIX_RT,
  2296. RTF_GATEWAY | RTF_DEFAULT);
  2297. if (rt) {
  2298. /* Autoconf prefix route */
  2299. if (valid_lft == 0) {
  2300. ip6_del_rt(net, rt);
  2301. rt = NULL;
  2302. } else if (addrconf_finite_timeout(rt_expires)) {
  2303. /* not infinity */
  2304. fib6_set_expires(rt, jiffies + rt_expires);
  2305. } else {
  2306. fib6_clean_expires(rt);
  2307. }
  2308. } else if (valid_lft) {
  2309. clock_t expires = 0;
  2310. int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
  2311. if (addrconf_finite_timeout(rt_expires)) {
  2312. /* not infinity */
  2313. flags |= RTF_EXPIRES;
  2314. expires = jiffies_to_clock_t(rt_expires);
  2315. }
  2316. addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
  2317. 0, dev, expires, flags,
  2318. GFP_ATOMIC);
  2319. }
  2320. fib6_info_release(rt);
  2321. }
  2322. /* Try to figure out our local address for this prefix */
  2323. if (pinfo->autoconf && in6_dev->cnf.autoconf) {
  2324. struct in6_addr addr;
  2325. bool tokenized = false, dev_addr_generated = false;
  2326. if (pinfo->prefix_len == 64) {
  2327. memcpy(&addr, &pinfo->prefix, 8);
  2328. if (!ipv6_addr_any(&in6_dev->token)) {
  2329. read_lock_bh(&in6_dev->lock);
  2330. memcpy(addr.s6_addr + 8,
  2331. in6_dev->token.s6_addr + 8, 8);
  2332. read_unlock_bh(&in6_dev->lock);
  2333. tokenized = true;
  2334. } else if (is_addr_mode_generate_stable(in6_dev) &&
  2335. !ipv6_generate_stable_address(&addr, 0,
  2336. in6_dev)) {
  2337. addr_flags |= IFA_F_STABLE_PRIVACY;
  2338. goto ok;
  2339. } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
  2340. ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
  2341. goto put;
  2342. } else {
  2343. dev_addr_generated = true;
  2344. }
  2345. goto ok;
  2346. }
  2347. net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
  2348. pinfo->prefix_len);
  2349. goto put;
  2350. ok:
  2351. err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
  2352. &addr, addr_type,
  2353. addr_flags, sllao,
  2354. tokenized, valid_lft,
  2355. prefered_lft);
  2356. if (err)
  2357. goto put;
  2358. /* Ignore error case here because previous prefix add addr was
  2359. * successful which will be notified.
  2360. */
  2361. ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
  2362. addr_type, addr_flags, sllao,
  2363. tokenized, valid_lft,
  2364. prefered_lft,
  2365. dev_addr_generated);
  2366. }
  2367. inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
  2368. put:
  2369. in6_dev_put(in6_dev);
  2370. }
  2371. /*
  2372. * Set destination address.
  2373. * Special case for SIT interfaces where we create a new "virtual"
  2374. * device.
  2375. */
  2376. int addrconf_set_dstaddr(struct net *net, void __user *arg)
  2377. {
  2378. struct in6_ifreq ireq;
  2379. struct net_device *dev;
  2380. int err = -EINVAL;
  2381. rtnl_lock();
  2382. err = -EFAULT;
  2383. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2384. goto err_exit;
  2385. dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
  2386. err = -ENODEV;
  2387. if (!dev)
  2388. goto err_exit;
  2389. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2390. if (dev->type == ARPHRD_SIT) {
  2391. const struct net_device_ops *ops = dev->netdev_ops;
  2392. struct ifreq ifr;
  2393. struct ip_tunnel_parm p;
  2394. err = -EADDRNOTAVAIL;
  2395. if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
  2396. goto err_exit;
  2397. memset(&p, 0, sizeof(p));
  2398. p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
  2399. p.iph.saddr = 0;
  2400. p.iph.version = 4;
  2401. p.iph.ihl = 5;
  2402. p.iph.protocol = IPPROTO_IPV6;
  2403. p.iph.ttl = 64;
  2404. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  2405. if (ops->ndo_do_ioctl) {
  2406. mm_segment_t oldfs = get_fs();
  2407. set_fs(KERNEL_DS);
  2408. err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  2409. set_fs(oldfs);
  2410. } else
  2411. err = -EOPNOTSUPP;
  2412. if (err == 0) {
  2413. err = -ENOBUFS;
  2414. dev = __dev_get_by_name(net, p.name);
  2415. if (!dev)
  2416. goto err_exit;
  2417. err = dev_open(dev);
  2418. }
  2419. }
  2420. #endif
  2421. err_exit:
  2422. rtnl_unlock();
  2423. return err;
  2424. }
  2425. static int ipv6_mc_config(struct sock *sk, bool join,
  2426. const struct in6_addr *addr, int ifindex)
  2427. {
  2428. int ret;
  2429. ASSERT_RTNL();
  2430. lock_sock(sk);
  2431. if (join)
  2432. ret = ipv6_sock_mc_join(sk, ifindex, addr);
  2433. else
  2434. ret = ipv6_sock_mc_drop(sk, ifindex, addr);
  2435. release_sock(sk);
  2436. return ret;
  2437. }
  2438. /*
  2439. * Manual configuration of address on an interface
  2440. */
  2441. static int inet6_addr_add(struct net *net, int ifindex,
  2442. struct ifa6_config *cfg,
  2443. struct netlink_ext_ack *extack)
  2444. {
  2445. struct inet6_ifaddr *ifp;
  2446. struct inet6_dev *idev;
  2447. struct net_device *dev;
  2448. unsigned long timeout;
  2449. clock_t expires;
  2450. u32 flags;
  2451. ASSERT_RTNL();
  2452. if (cfg->plen > 128)
  2453. return -EINVAL;
  2454. /* check the lifetime */
  2455. if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
  2456. return -EINVAL;
  2457. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
  2458. return -EINVAL;
  2459. dev = __dev_get_by_index(net, ifindex);
  2460. if (!dev)
  2461. return -ENODEV;
  2462. idev = addrconf_add_dev(dev);
  2463. if (IS_ERR(idev))
  2464. return PTR_ERR(idev);
  2465. if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
  2466. int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
  2467. true, cfg->pfx, ifindex);
  2468. if (ret < 0)
  2469. return ret;
  2470. }
  2471. cfg->scope = ipv6_addr_scope(cfg->pfx);
  2472. timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
  2473. if (addrconf_finite_timeout(timeout)) {
  2474. expires = jiffies_to_clock_t(timeout * HZ);
  2475. cfg->valid_lft = timeout;
  2476. flags = RTF_EXPIRES;
  2477. } else {
  2478. expires = 0;
  2479. flags = 0;
  2480. cfg->ifa_flags |= IFA_F_PERMANENT;
  2481. }
  2482. timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
  2483. if (addrconf_finite_timeout(timeout)) {
  2484. if (timeout == 0)
  2485. cfg->ifa_flags |= IFA_F_DEPRECATED;
  2486. cfg->preferred_lft = timeout;
  2487. }
  2488. ifp = ipv6_add_addr(idev, cfg, true, extack);
  2489. if (!IS_ERR(ifp)) {
  2490. if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
  2491. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  2492. ifp->rt_priority, dev, expires,
  2493. flags, GFP_KERNEL);
  2494. }
  2495. /* Send a netlink notification if DAD is enabled and
  2496. * optimistic flag is not set
  2497. */
  2498. if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
  2499. ipv6_ifa_notify(0, ifp);
  2500. /*
  2501. * Note that section 3.1 of RFC 4429 indicates
  2502. * that the Optimistic flag should not be set for
  2503. * manually configured addresses
  2504. */
  2505. addrconf_dad_start(ifp);
  2506. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
  2507. manage_tempaddrs(idev, ifp, cfg->valid_lft,
  2508. cfg->preferred_lft, true, jiffies);
  2509. in6_ifa_put(ifp);
  2510. addrconf_verify_rtnl();
  2511. return 0;
  2512. } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
  2513. ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
  2514. cfg->pfx, ifindex);
  2515. }
  2516. return PTR_ERR(ifp);
  2517. }
  2518. static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
  2519. const struct in6_addr *pfx, unsigned int plen)
  2520. {
  2521. struct inet6_ifaddr *ifp;
  2522. struct inet6_dev *idev;
  2523. struct net_device *dev;
  2524. if (plen > 128)
  2525. return -EINVAL;
  2526. dev = __dev_get_by_index(net, ifindex);
  2527. if (!dev)
  2528. return -ENODEV;
  2529. idev = __in6_dev_get(dev);
  2530. if (!idev)
  2531. return -ENXIO;
  2532. read_lock_bh(&idev->lock);
  2533. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  2534. if (ifp->prefix_len == plen &&
  2535. ipv6_addr_equal(pfx, &ifp->addr)) {
  2536. in6_ifa_hold(ifp);
  2537. read_unlock_bh(&idev->lock);
  2538. if (!(ifp->flags & IFA_F_TEMPORARY) &&
  2539. (ifa_flags & IFA_F_MANAGETEMPADDR))
  2540. manage_tempaddrs(idev, ifp, 0, 0, false,
  2541. jiffies);
  2542. ipv6_del_addr(ifp);
  2543. addrconf_verify_rtnl();
  2544. if (ipv6_addr_is_multicast(pfx)) {
  2545. ipv6_mc_config(net->ipv6.mc_autojoin_sk,
  2546. false, pfx, dev->ifindex);
  2547. }
  2548. return 0;
  2549. }
  2550. }
  2551. read_unlock_bh(&idev->lock);
  2552. return -EADDRNOTAVAIL;
  2553. }
  2554. int addrconf_add_ifaddr(struct net *net, void __user *arg)
  2555. {
  2556. struct ifa6_config cfg = {
  2557. .ifa_flags = IFA_F_PERMANENT,
  2558. .preferred_lft = INFINITY_LIFE_TIME,
  2559. .valid_lft = INFINITY_LIFE_TIME,
  2560. };
  2561. struct in6_ifreq ireq;
  2562. int err;
  2563. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2564. return -EPERM;
  2565. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2566. return -EFAULT;
  2567. cfg.pfx = &ireq.ifr6_addr;
  2568. cfg.plen = ireq.ifr6_prefixlen;
  2569. rtnl_lock();
  2570. err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
  2571. rtnl_unlock();
  2572. return err;
  2573. }
  2574. int addrconf_del_ifaddr(struct net *net, void __user *arg)
  2575. {
  2576. struct in6_ifreq ireq;
  2577. int err;
  2578. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2579. return -EPERM;
  2580. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2581. return -EFAULT;
  2582. rtnl_lock();
  2583. err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
  2584. ireq.ifr6_prefixlen);
  2585. rtnl_unlock();
  2586. return err;
  2587. }
  2588. static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
  2589. int plen, int scope)
  2590. {
  2591. struct inet6_ifaddr *ifp;
  2592. struct ifa6_config cfg = {
  2593. .pfx = addr,
  2594. .plen = plen,
  2595. .ifa_flags = IFA_F_PERMANENT,
  2596. .valid_lft = INFINITY_LIFE_TIME,
  2597. .preferred_lft = INFINITY_LIFE_TIME,
  2598. .scope = scope
  2599. };
  2600. ifp = ipv6_add_addr(idev, &cfg, true, NULL);
  2601. if (!IS_ERR(ifp)) {
  2602. spin_lock_bh(&ifp->lock);
  2603. ifp->flags &= ~IFA_F_TENTATIVE;
  2604. spin_unlock_bh(&ifp->lock);
  2605. rt_genid_bump_ipv6(dev_net(idev->dev));
  2606. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2607. in6_ifa_put(ifp);
  2608. }
  2609. }
  2610. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2611. static void sit_add_v4_addrs(struct inet6_dev *idev)
  2612. {
  2613. struct in6_addr addr;
  2614. struct net_device *dev;
  2615. struct net *net = dev_net(idev->dev);
  2616. int scope, plen;
  2617. u32 pflags = 0;
  2618. ASSERT_RTNL();
  2619. memset(&addr, 0, sizeof(struct in6_addr));
  2620. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  2621. if (idev->dev->flags&IFF_POINTOPOINT) {
  2622. addr.s6_addr32[0] = htonl(0xfe800000);
  2623. scope = IFA_LINK;
  2624. plen = 64;
  2625. } else {
  2626. scope = IPV6_ADDR_COMPATv4;
  2627. plen = 96;
  2628. pflags |= RTF_NONEXTHOP;
  2629. }
  2630. if (addr.s6_addr32[3]) {
  2631. add_addr(idev, &addr, plen, scope);
  2632. addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
  2633. GFP_KERNEL);
  2634. return;
  2635. }
  2636. for_each_netdev(net, dev) {
  2637. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  2638. if (in_dev && (dev->flags & IFF_UP)) {
  2639. struct in_ifaddr *ifa;
  2640. int flag = scope;
  2641. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  2642. addr.s6_addr32[3] = ifa->ifa_local;
  2643. if (ifa->ifa_scope == RT_SCOPE_LINK)
  2644. continue;
  2645. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  2646. if (idev->dev->flags&IFF_POINTOPOINT)
  2647. continue;
  2648. flag |= IFA_HOST;
  2649. }
  2650. add_addr(idev, &addr, plen, flag);
  2651. addrconf_prefix_route(&addr, plen, 0, idev->dev,
  2652. 0, pflags, GFP_KERNEL);
  2653. }
  2654. }
  2655. }
  2656. }
  2657. #endif
  2658. static void init_loopback(struct net_device *dev)
  2659. {
  2660. struct inet6_dev *idev;
  2661. /* ::1 */
  2662. ASSERT_RTNL();
  2663. idev = ipv6_find_idev(dev);
  2664. if (!idev) {
  2665. pr_debug("%s: add_dev failed\n", __func__);
  2666. return;
  2667. }
  2668. add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
  2669. }
  2670. void addrconf_add_linklocal(struct inet6_dev *idev,
  2671. const struct in6_addr *addr, u32 flags)
  2672. {
  2673. struct ifa6_config cfg = {
  2674. .pfx = addr,
  2675. .plen = 64,
  2676. .ifa_flags = flags | IFA_F_PERMANENT,
  2677. .valid_lft = INFINITY_LIFE_TIME,
  2678. .preferred_lft = INFINITY_LIFE_TIME,
  2679. .scope = IFA_LINK
  2680. };
  2681. struct inet6_ifaddr *ifp;
  2682. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2683. if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
  2684. idev->cnf.optimistic_dad) &&
  2685. !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
  2686. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  2687. #endif
  2688. ifp = ipv6_add_addr(idev, &cfg, true, NULL);
  2689. if (!IS_ERR(ifp)) {
  2690. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
  2691. 0, 0, GFP_ATOMIC);
  2692. addrconf_dad_start(ifp);
  2693. in6_ifa_put(ifp);
  2694. }
  2695. }
  2696. EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
  2697. static bool ipv6_reserved_interfaceid(struct in6_addr address)
  2698. {
  2699. if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
  2700. return true;
  2701. if (address.s6_addr32[2] == htonl(0x02005eff) &&
  2702. ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
  2703. return true;
  2704. if (address.s6_addr32[2] == htonl(0xfdffffff) &&
  2705. ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
  2706. return true;
  2707. return false;
  2708. }
  2709. static int ipv6_generate_stable_address(struct in6_addr *address,
  2710. u8 dad_count,
  2711. const struct inet6_dev *idev)
  2712. {
  2713. static DEFINE_SPINLOCK(lock);
  2714. static __u32 digest[SHA_DIGEST_WORDS];
  2715. static __u32 workspace[SHA_WORKSPACE_WORDS];
  2716. static union {
  2717. char __data[SHA_MESSAGE_BYTES];
  2718. struct {
  2719. struct in6_addr secret;
  2720. __be32 prefix[2];
  2721. unsigned char hwaddr[MAX_ADDR_LEN];
  2722. u8 dad_count;
  2723. } __packed;
  2724. } data;
  2725. struct in6_addr secret;
  2726. struct in6_addr temp;
  2727. struct net *net = dev_net(idev->dev);
  2728. BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
  2729. if (idev->cnf.stable_secret.initialized)
  2730. secret = idev->cnf.stable_secret.secret;
  2731. else if (net->ipv6.devconf_dflt->stable_secret.initialized)
  2732. secret = net->ipv6.devconf_dflt->stable_secret.secret;
  2733. else
  2734. return -1;
  2735. retry:
  2736. spin_lock_bh(&lock);
  2737. sha_init(digest);
  2738. memset(&data, 0, sizeof(data));
  2739. memset(workspace, 0, sizeof(workspace));
  2740. memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
  2741. data.prefix[0] = address->s6_addr32[0];
  2742. data.prefix[1] = address->s6_addr32[1];
  2743. data.secret = secret;
  2744. data.dad_count = dad_count;
  2745. sha_transform(digest, data.__data, workspace);
  2746. temp = *address;
  2747. temp.s6_addr32[2] = (__force __be32)digest[0];
  2748. temp.s6_addr32[3] = (__force __be32)digest[1];
  2749. spin_unlock_bh(&lock);
  2750. if (ipv6_reserved_interfaceid(temp)) {
  2751. dad_count++;
  2752. if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
  2753. return -1;
  2754. goto retry;
  2755. }
  2756. *address = temp;
  2757. return 0;
  2758. }
  2759. static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
  2760. {
  2761. struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
  2762. if (s->initialized)
  2763. return;
  2764. s = &idev->cnf.stable_secret;
  2765. get_random_bytes(&s->secret, sizeof(s->secret));
  2766. s->initialized = true;
  2767. }
  2768. static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
  2769. {
  2770. struct in6_addr addr;
  2771. /* no link local addresses on L3 master devices */
  2772. if (netif_is_l3_master(idev->dev))
  2773. return;
  2774. /* no link local addresses on devices flagged as slaves */
  2775. if (idev->dev->flags & IFF_SLAVE)
  2776. return;
  2777. ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
  2778. switch (idev->cnf.addr_gen_mode) {
  2779. case IN6_ADDR_GEN_MODE_RANDOM:
  2780. ipv6_gen_mode_random_init(idev);
  2781. /* fallthrough */
  2782. case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
  2783. if (!ipv6_generate_stable_address(&addr, 0, idev))
  2784. addrconf_add_linklocal(idev, &addr,
  2785. IFA_F_STABLE_PRIVACY);
  2786. else if (prefix_route)
  2787. addrconf_prefix_route(&addr, 64, 0, idev->dev,
  2788. 0, 0, GFP_KERNEL);
  2789. break;
  2790. case IN6_ADDR_GEN_MODE_EUI64:
  2791. /* addrconf_add_linklocal also adds a prefix_route and we
  2792. * only need to care about prefix routes if ipv6_generate_eui64
  2793. * couldn't generate one.
  2794. */
  2795. if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
  2796. addrconf_add_linklocal(idev, &addr, 0);
  2797. else if (prefix_route)
  2798. addrconf_prefix_route(&addr, 64, 0, idev->dev,
  2799. 0, 0, GFP_KERNEL);
  2800. break;
  2801. case IN6_ADDR_GEN_MODE_NONE:
  2802. default:
  2803. /* will not add any link local address */
  2804. break;
  2805. }
  2806. }
  2807. static void addrconf_dev_config(struct net_device *dev)
  2808. {
  2809. struct inet6_dev *idev;
  2810. ASSERT_RTNL();
  2811. if ((dev->type != ARPHRD_ETHER) &&
  2812. (dev->type != ARPHRD_FDDI) &&
  2813. (dev->type != ARPHRD_ARCNET) &&
  2814. (dev->type != ARPHRD_INFINIBAND) &&
  2815. (dev->type != ARPHRD_IEEE1394) &&
  2816. (dev->type != ARPHRD_TUNNEL6) &&
  2817. (dev->type != ARPHRD_6LOWPAN) &&
  2818. (dev->type != ARPHRD_IP6GRE) &&
  2819. (dev->type != ARPHRD_IPGRE) &&
  2820. (dev->type != ARPHRD_TUNNEL) &&
  2821. (dev->type != ARPHRD_NONE) &&
  2822. (dev->type != ARPHRD_RAWIP)) {
  2823. /* Alas, we support only Ethernet autoconfiguration. */
  2824. idev = __in6_dev_get(dev);
  2825. if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
  2826. dev->flags & IFF_MULTICAST)
  2827. ipv6_mc_up(idev);
  2828. return;
  2829. }
  2830. idev = addrconf_add_dev(dev);
  2831. if (IS_ERR(idev))
  2832. return;
  2833. /* this device type has no EUI support */
  2834. if (dev->type == ARPHRD_NONE &&
  2835. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
  2836. idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
  2837. addrconf_addr_gen(idev, false);
  2838. }
  2839. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2840. static void addrconf_sit_config(struct net_device *dev)
  2841. {
  2842. struct inet6_dev *idev;
  2843. ASSERT_RTNL();
  2844. /*
  2845. * Configure the tunnel with one of our IPv4
  2846. * addresses... we should configure all of
  2847. * our v4 addrs in the tunnel
  2848. */
  2849. idev = ipv6_find_idev(dev);
  2850. if (!idev) {
  2851. pr_debug("%s: add_dev failed\n", __func__);
  2852. return;
  2853. }
  2854. if (dev->priv_flags & IFF_ISATAP) {
  2855. addrconf_addr_gen(idev, false);
  2856. return;
  2857. }
  2858. sit_add_v4_addrs(idev);
  2859. if (dev->flags&IFF_POINTOPOINT)
  2860. addrconf_add_mroute(dev);
  2861. }
  2862. #endif
  2863. #if IS_ENABLED(CONFIG_NET_IPGRE)
  2864. static void addrconf_gre_config(struct net_device *dev)
  2865. {
  2866. struct inet6_dev *idev;
  2867. ASSERT_RTNL();
  2868. idev = ipv6_find_idev(dev);
  2869. if (!idev) {
  2870. pr_debug("%s: add_dev failed\n", __func__);
  2871. return;
  2872. }
  2873. addrconf_addr_gen(idev, true);
  2874. if (dev->flags & IFF_POINTOPOINT)
  2875. addrconf_add_mroute(dev);
  2876. }
  2877. #endif
  2878. static int fixup_permanent_addr(struct net *net,
  2879. struct inet6_dev *idev,
  2880. struct inet6_ifaddr *ifp)
  2881. {
  2882. /* !fib6_node means the host route was removed from the
  2883. * FIB, for example, if 'lo' device is taken down. In that
  2884. * case regenerate the host route.
  2885. */
  2886. if (!ifp->rt || !ifp->rt->fib6_node) {
  2887. struct fib6_info *f6i, *prev;
  2888. f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
  2889. GFP_ATOMIC);
  2890. if (IS_ERR(f6i))
  2891. return PTR_ERR(f6i);
  2892. /* ifp->rt can be accessed outside of rtnl */
  2893. spin_lock(&ifp->lock);
  2894. prev = ifp->rt;
  2895. ifp->rt = f6i;
  2896. spin_unlock(&ifp->lock);
  2897. fib6_info_release(prev);
  2898. }
  2899. if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
  2900. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  2901. ifp->rt_priority, idev->dev, 0, 0,
  2902. GFP_ATOMIC);
  2903. }
  2904. if (ifp->state == INET6_IFADDR_STATE_PREDAD)
  2905. addrconf_dad_start(ifp);
  2906. return 0;
  2907. }
  2908. static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
  2909. {
  2910. struct inet6_ifaddr *ifp, *tmp;
  2911. struct inet6_dev *idev;
  2912. idev = __in6_dev_get(dev);
  2913. if (!idev)
  2914. return;
  2915. write_lock_bh(&idev->lock);
  2916. list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
  2917. if ((ifp->flags & IFA_F_PERMANENT) &&
  2918. fixup_permanent_addr(net, idev, ifp) < 0) {
  2919. write_unlock_bh(&idev->lock);
  2920. in6_ifa_hold(ifp);
  2921. ipv6_del_addr(ifp);
  2922. write_lock_bh(&idev->lock);
  2923. net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
  2924. idev->dev->name, &ifp->addr);
  2925. }
  2926. }
  2927. write_unlock_bh(&idev->lock);
  2928. }
  2929. static int addrconf_notify(struct notifier_block *this, unsigned long event,
  2930. void *ptr)
  2931. {
  2932. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2933. struct netdev_notifier_change_info *change_info;
  2934. struct netdev_notifier_changeupper_info *info;
  2935. struct inet6_dev *idev = __in6_dev_get(dev);
  2936. struct net *net = dev_net(dev);
  2937. int run_pending = 0;
  2938. int err;
  2939. switch (event) {
  2940. case NETDEV_REGISTER:
  2941. if (!idev && dev->mtu >= IPV6_MIN_MTU) {
  2942. idev = ipv6_add_dev(dev);
  2943. if (IS_ERR(idev))
  2944. return notifier_from_errno(PTR_ERR(idev));
  2945. }
  2946. break;
  2947. case NETDEV_CHANGEMTU:
  2948. /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
  2949. if (dev->mtu < IPV6_MIN_MTU) {
  2950. addrconf_ifdown(dev, dev != net->loopback_dev);
  2951. break;
  2952. }
  2953. if (idev) {
  2954. rt6_mtu_change(dev, dev->mtu);
  2955. idev->cnf.mtu6 = dev->mtu;
  2956. break;
  2957. }
  2958. /* allocate new idev */
  2959. idev = ipv6_add_dev(dev);
  2960. if (IS_ERR(idev))
  2961. break;
  2962. /* device is still not ready */
  2963. if (!(idev->if_flags & IF_READY))
  2964. break;
  2965. run_pending = 1;
  2966. /* fall through */
  2967. case NETDEV_UP:
  2968. case NETDEV_CHANGE:
  2969. if (dev->flags & IFF_SLAVE)
  2970. break;
  2971. if (idev && idev->cnf.disable_ipv6)
  2972. break;
  2973. if (event == NETDEV_UP) {
  2974. /* restore routes for permanent addresses */
  2975. addrconf_permanent_addr(net, dev);
  2976. if (!addrconf_link_ready(dev)) {
  2977. /* device is not ready yet. */
  2978. pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
  2979. dev->name);
  2980. break;
  2981. }
  2982. if (!idev && dev->mtu >= IPV6_MIN_MTU)
  2983. idev = ipv6_add_dev(dev);
  2984. if (!IS_ERR_OR_NULL(idev)) {
  2985. idev->if_flags |= IF_READY;
  2986. run_pending = 1;
  2987. }
  2988. } else if (event == NETDEV_CHANGE) {
  2989. if (!addrconf_link_ready(dev)) {
  2990. /* device is still not ready. */
  2991. rt6_sync_down_dev(dev, event);
  2992. break;
  2993. }
  2994. if (!IS_ERR_OR_NULL(idev)) {
  2995. if (idev->if_flags & IF_READY) {
  2996. /* device is already configured -
  2997. * but resend MLD reports, we might
  2998. * have roamed and need to update
  2999. * multicast snooping switches
  3000. */
  3001. ipv6_mc_up(idev);
  3002. change_info = ptr;
  3003. if (change_info->flags_changed & IFF_NOARP)
  3004. addrconf_dad_run(idev, true);
  3005. rt6_sync_up(dev, RTNH_F_LINKDOWN);
  3006. break;
  3007. }
  3008. idev->if_flags |= IF_READY;
  3009. }
  3010. pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
  3011. dev->name);
  3012. run_pending = 1;
  3013. }
  3014. switch (dev->type) {
  3015. #if IS_ENABLED(CONFIG_IPV6_SIT)
  3016. case ARPHRD_SIT:
  3017. addrconf_sit_config(dev);
  3018. break;
  3019. #endif
  3020. #if IS_ENABLED(CONFIG_NET_IPGRE)
  3021. case ARPHRD_IPGRE:
  3022. addrconf_gre_config(dev);
  3023. break;
  3024. #endif
  3025. case ARPHRD_LOOPBACK:
  3026. init_loopback(dev);
  3027. break;
  3028. default:
  3029. addrconf_dev_config(dev);
  3030. break;
  3031. }
  3032. if (!IS_ERR_OR_NULL(idev)) {
  3033. if (run_pending)
  3034. addrconf_dad_run(idev, false);
  3035. /* Device has an address by now */
  3036. rt6_sync_up(dev, RTNH_F_DEAD);
  3037. /*
  3038. * If the MTU changed during the interface down,
  3039. * when the interface up, the changed MTU must be
  3040. * reflected in the idev as well as routers.
  3041. */
  3042. if (idev->cnf.mtu6 != dev->mtu &&
  3043. dev->mtu >= IPV6_MIN_MTU) {
  3044. rt6_mtu_change(dev, dev->mtu);
  3045. idev->cnf.mtu6 = dev->mtu;
  3046. }
  3047. idev->tstamp = jiffies;
  3048. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  3049. /*
  3050. * If the changed mtu during down is lower than
  3051. * IPV6_MIN_MTU stop IPv6 on this interface.
  3052. */
  3053. if (dev->mtu < IPV6_MIN_MTU)
  3054. addrconf_ifdown(dev, dev != net->loopback_dev);
  3055. }
  3056. break;
  3057. case NETDEV_DOWN:
  3058. case NETDEV_UNREGISTER:
  3059. /*
  3060. * Remove all addresses from this interface.
  3061. */
  3062. addrconf_ifdown(dev, event != NETDEV_DOWN);
  3063. break;
  3064. case NETDEV_CHANGENAME:
  3065. if (idev) {
  3066. snmp6_unregister_dev(idev);
  3067. addrconf_sysctl_unregister(idev);
  3068. err = addrconf_sysctl_register(idev);
  3069. if (err)
  3070. return notifier_from_errno(err);
  3071. err = snmp6_register_dev(idev);
  3072. if (err) {
  3073. addrconf_sysctl_unregister(idev);
  3074. return notifier_from_errno(err);
  3075. }
  3076. }
  3077. break;
  3078. case NETDEV_PRE_TYPE_CHANGE:
  3079. case NETDEV_POST_TYPE_CHANGE:
  3080. if (idev)
  3081. addrconf_type_change(dev, event);
  3082. break;
  3083. case NETDEV_CHANGEUPPER:
  3084. info = ptr;
  3085. /* flush all routes if dev is linked to or unlinked from
  3086. * an L3 master device (e.g., VRF)
  3087. */
  3088. if (info->upper_dev && netif_is_l3_master(info->upper_dev))
  3089. addrconf_ifdown(dev, 0);
  3090. }
  3091. return NOTIFY_OK;
  3092. }
  3093. /*
  3094. * addrconf module should be notified of a device going up
  3095. */
  3096. static struct notifier_block ipv6_dev_notf = {
  3097. .notifier_call = addrconf_notify,
  3098. .priority = ADDRCONF_NOTIFY_PRIORITY,
  3099. };
  3100. static void addrconf_type_change(struct net_device *dev, unsigned long event)
  3101. {
  3102. struct inet6_dev *idev;
  3103. ASSERT_RTNL();
  3104. idev = __in6_dev_get(dev);
  3105. if (event == NETDEV_POST_TYPE_CHANGE)
  3106. ipv6_mc_remap(idev);
  3107. else if (event == NETDEV_PRE_TYPE_CHANGE)
  3108. ipv6_mc_unmap(idev);
  3109. }
  3110. static bool addr_is_local(const struct in6_addr *addr)
  3111. {
  3112. return ipv6_addr_type(addr) &
  3113. (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
  3114. }
  3115. static int addrconf_ifdown(struct net_device *dev, int how)
  3116. {
  3117. unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
  3118. struct net *net = dev_net(dev);
  3119. struct inet6_dev *idev;
  3120. struct inet6_ifaddr *ifa, *tmp;
  3121. bool keep_addr = false;
  3122. int state, i;
  3123. ASSERT_RTNL();
  3124. rt6_disable_ip(dev, event);
  3125. idev = __in6_dev_get(dev);
  3126. if (!idev)
  3127. return -ENODEV;
  3128. /*
  3129. * Step 1: remove reference to ipv6 device from parent device.
  3130. * Do not dev_put!
  3131. */
  3132. if (how) {
  3133. idev->dead = 1;
  3134. /* protected by rtnl_lock */
  3135. RCU_INIT_POINTER(dev->ip6_ptr, NULL);
  3136. /* Step 1.5: remove snmp6 entry */
  3137. snmp6_unregister_dev(idev);
  3138. }
  3139. /* combine the user config with event to determine if permanent
  3140. * addresses are to be removed from address hash table
  3141. */
  3142. if (!how && !idev->cnf.disable_ipv6) {
  3143. /* aggregate the system setting and interface setting */
  3144. int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
  3145. if (!_keep_addr)
  3146. _keep_addr = idev->cnf.keep_addr_on_down;
  3147. keep_addr = (_keep_addr > 0);
  3148. }
  3149. /* Step 2: clear hash table */
  3150. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  3151. struct hlist_head *h = &inet6_addr_lst[i];
  3152. spin_lock_bh(&addrconf_hash_lock);
  3153. restart:
  3154. hlist_for_each_entry_rcu(ifa, h, addr_lst) {
  3155. if (ifa->idev == idev) {
  3156. addrconf_del_dad_work(ifa);
  3157. /* combined flag + permanent flag decide if
  3158. * address is retained on a down event
  3159. */
  3160. if (!keep_addr ||
  3161. !(ifa->flags & IFA_F_PERMANENT) ||
  3162. addr_is_local(&ifa->addr)) {
  3163. hlist_del_init_rcu(&ifa->addr_lst);
  3164. goto restart;
  3165. }
  3166. }
  3167. }
  3168. spin_unlock_bh(&addrconf_hash_lock);
  3169. }
  3170. write_lock_bh(&idev->lock);
  3171. addrconf_del_rs_timer(idev);
  3172. /* Step 2: clear flags for stateless addrconf */
  3173. if (!how)
  3174. idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
  3175. /* Step 3: clear tempaddr list */
  3176. while (!list_empty(&idev->tempaddr_list)) {
  3177. ifa = list_first_entry(&idev->tempaddr_list,
  3178. struct inet6_ifaddr, tmp_list);
  3179. list_del(&ifa->tmp_list);
  3180. write_unlock_bh(&idev->lock);
  3181. spin_lock_bh(&ifa->lock);
  3182. if (ifa->ifpub) {
  3183. in6_ifa_put(ifa->ifpub);
  3184. ifa->ifpub = NULL;
  3185. }
  3186. spin_unlock_bh(&ifa->lock);
  3187. in6_ifa_put(ifa);
  3188. write_lock_bh(&idev->lock);
  3189. }
  3190. list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
  3191. struct fib6_info *rt = NULL;
  3192. bool keep;
  3193. addrconf_del_dad_work(ifa);
  3194. keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
  3195. !addr_is_local(&ifa->addr);
  3196. write_unlock_bh(&idev->lock);
  3197. spin_lock_bh(&ifa->lock);
  3198. if (keep) {
  3199. /* set state to skip the notifier below */
  3200. state = INET6_IFADDR_STATE_DEAD;
  3201. ifa->state = INET6_IFADDR_STATE_PREDAD;
  3202. if (!(ifa->flags & IFA_F_NODAD))
  3203. ifa->flags |= IFA_F_TENTATIVE;
  3204. rt = ifa->rt;
  3205. ifa->rt = NULL;
  3206. } else {
  3207. state = ifa->state;
  3208. ifa->state = INET6_IFADDR_STATE_DEAD;
  3209. }
  3210. spin_unlock_bh(&ifa->lock);
  3211. if (rt)
  3212. ip6_del_rt(net, rt);
  3213. if (state != INET6_IFADDR_STATE_DEAD) {
  3214. __ipv6_ifa_notify(RTM_DELADDR, ifa);
  3215. inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
  3216. } else {
  3217. if (idev->cnf.forwarding)
  3218. addrconf_leave_anycast(ifa);
  3219. addrconf_leave_solict(ifa->idev, &ifa->addr);
  3220. }
  3221. write_lock_bh(&idev->lock);
  3222. if (!keep) {
  3223. list_del_rcu(&ifa->if_list);
  3224. in6_ifa_put(ifa);
  3225. }
  3226. }
  3227. write_unlock_bh(&idev->lock);
  3228. /* Step 5: Discard anycast and multicast list */
  3229. if (how) {
  3230. ipv6_ac_destroy_dev(idev);
  3231. ipv6_mc_destroy_dev(idev);
  3232. } else {
  3233. ipv6_mc_down(idev);
  3234. }
  3235. idev->tstamp = jiffies;
  3236. /* Last: Shot the device (if unregistered) */
  3237. if (how) {
  3238. addrconf_sysctl_unregister(idev);
  3239. neigh_parms_release(&nd_tbl, idev->nd_parms);
  3240. neigh_ifdown(&nd_tbl, dev);
  3241. in6_dev_put(idev);
  3242. }
  3243. return 0;
  3244. }
  3245. static void addrconf_rs_timer(struct timer_list *t)
  3246. {
  3247. struct inet6_dev *idev = from_timer(idev, t, rs_timer);
  3248. struct net_device *dev = idev->dev;
  3249. struct in6_addr lladdr;
  3250. write_lock(&idev->lock);
  3251. if (idev->dead || !(idev->if_flags & IF_READY))
  3252. goto out;
  3253. if (!ipv6_accept_ra(idev))
  3254. goto out;
  3255. /* Announcement received after solicitation was sent */
  3256. if (idev->if_flags & IF_RA_RCVD)
  3257. goto out;
  3258. if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
  3259. write_unlock(&idev->lock);
  3260. if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  3261. ndisc_send_rs(dev, &lladdr,
  3262. &in6addr_linklocal_allrouters);
  3263. else
  3264. goto put;
  3265. write_lock(&idev->lock);
  3266. idev->rs_interval = rfc3315_s14_backoff_update(
  3267. idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
  3268. /* The wait after the last probe can be shorter */
  3269. addrconf_mod_rs_timer(idev, (idev->rs_probes ==
  3270. idev->cnf.rtr_solicits) ?
  3271. idev->cnf.rtr_solicit_delay :
  3272. idev->rs_interval);
  3273. } else {
  3274. /*
  3275. * Note: we do not support deprecated "all on-link"
  3276. * assumption any longer.
  3277. */
  3278. pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
  3279. }
  3280. out:
  3281. write_unlock(&idev->lock);
  3282. put:
  3283. in6_dev_put(idev);
  3284. }
  3285. /*
  3286. * Duplicate Address Detection
  3287. */
  3288. static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
  3289. {
  3290. unsigned long rand_num;
  3291. struct inet6_dev *idev = ifp->idev;
  3292. u64 nonce;
  3293. if (ifp->flags & IFA_F_OPTIMISTIC)
  3294. rand_num = 0;
  3295. else
  3296. rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
  3297. nonce = 0;
  3298. if (idev->cnf.enhanced_dad ||
  3299. dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
  3300. do
  3301. get_random_bytes(&nonce, 6);
  3302. while (nonce == 0);
  3303. }
  3304. ifp->dad_nonce = nonce;
  3305. ifp->dad_probes = idev->cnf.dad_transmits;
  3306. addrconf_mod_dad_work(ifp, rand_num);
  3307. }
  3308. static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
  3309. {
  3310. struct inet6_dev *idev = ifp->idev;
  3311. struct net_device *dev = idev->dev;
  3312. bool bump_id, notify = false;
  3313. struct net *net;
  3314. addrconf_join_solict(dev, &ifp->addr);
  3315. prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
  3316. read_lock_bh(&idev->lock);
  3317. spin_lock(&ifp->lock);
  3318. if (ifp->state == INET6_IFADDR_STATE_DEAD)
  3319. goto out;
  3320. net = dev_net(dev);
  3321. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  3322. (net->ipv6.devconf_all->accept_dad < 1 &&
  3323. idev->cnf.accept_dad < 1) ||
  3324. !(ifp->flags&IFA_F_TENTATIVE) ||
  3325. ifp->flags & IFA_F_NODAD) {
  3326. bool send_na = false;
  3327. if (ifp->flags & IFA_F_TENTATIVE &&
  3328. !(ifp->flags & IFA_F_OPTIMISTIC))
  3329. send_na = true;
  3330. bump_id = ifp->flags & IFA_F_TENTATIVE;
  3331. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  3332. spin_unlock(&ifp->lock);
  3333. read_unlock_bh(&idev->lock);
  3334. addrconf_dad_completed(ifp, bump_id, send_na);
  3335. return;
  3336. }
  3337. if (!(idev->if_flags & IF_READY)) {
  3338. spin_unlock(&ifp->lock);
  3339. read_unlock_bh(&idev->lock);
  3340. /*
  3341. * If the device is not ready:
  3342. * - keep it tentative if it is a permanent address.
  3343. * - otherwise, kill it.
  3344. */
  3345. in6_ifa_hold(ifp);
  3346. addrconf_dad_stop(ifp, 0);
  3347. return;
  3348. }
  3349. /*
  3350. * Optimistic nodes can start receiving
  3351. * Frames right away
  3352. */
  3353. if (ifp->flags & IFA_F_OPTIMISTIC) {
  3354. ip6_ins_rt(net, ifp->rt);
  3355. if (ipv6_use_optimistic_addr(net, idev)) {
  3356. /* Because optimistic nodes can use this address,
  3357. * notify listeners. If DAD fails, RTM_DELADDR is sent.
  3358. */
  3359. notify = true;
  3360. }
  3361. }
  3362. addrconf_dad_kick(ifp);
  3363. out:
  3364. spin_unlock(&ifp->lock);
  3365. read_unlock_bh(&idev->lock);
  3366. if (notify)
  3367. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  3368. }
  3369. static void addrconf_dad_start(struct inet6_ifaddr *ifp)
  3370. {
  3371. bool begin_dad = false;
  3372. spin_lock_bh(&ifp->lock);
  3373. if (ifp->state != INET6_IFADDR_STATE_DEAD) {
  3374. ifp->state = INET6_IFADDR_STATE_PREDAD;
  3375. begin_dad = true;
  3376. }
  3377. spin_unlock_bh(&ifp->lock);
  3378. if (begin_dad)
  3379. addrconf_mod_dad_work(ifp, 0);
  3380. }
  3381. static void addrconf_dad_work(struct work_struct *w)
  3382. {
  3383. struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
  3384. struct inet6_ifaddr,
  3385. dad_work);
  3386. struct inet6_dev *idev = ifp->idev;
  3387. bool bump_id, disable_ipv6 = false;
  3388. struct in6_addr mcaddr;
  3389. enum {
  3390. DAD_PROCESS,
  3391. DAD_BEGIN,
  3392. DAD_ABORT,
  3393. } action = DAD_PROCESS;
  3394. rtnl_lock();
  3395. spin_lock_bh(&ifp->lock);
  3396. if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
  3397. action = DAD_BEGIN;
  3398. ifp->state = INET6_IFADDR_STATE_DAD;
  3399. } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
  3400. action = DAD_ABORT;
  3401. ifp->state = INET6_IFADDR_STATE_POSTDAD;
  3402. if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
  3403. idev->cnf.accept_dad > 1) &&
  3404. !idev->cnf.disable_ipv6 &&
  3405. !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
  3406. struct in6_addr addr;
  3407. addr.s6_addr32[0] = htonl(0xfe800000);
  3408. addr.s6_addr32[1] = 0;
  3409. if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
  3410. ipv6_addr_equal(&ifp->addr, &addr)) {
  3411. /* DAD failed for link-local based on MAC */
  3412. idev->cnf.disable_ipv6 = 1;
  3413. pr_info("%s: IPv6 being disabled!\n",
  3414. ifp->idev->dev->name);
  3415. disable_ipv6 = true;
  3416. }
  3417. }
  3418. }
  3419. spin_unlock_bh(&ifp->lock);
  3420. if (action == DAD_BEGIN) {
  3421. addrconf_dad_begin(ifp);
  3422. goto out;
  3423. } else if (action == DAD_ABORT) {
  3424. in6_ifa_hold(ifp);
  3425. addrconf_dad_stop(ifp, 1);
  3426. if (disable_ipv6)
  3427. addrconf_ifdown(idev->dev, 0);
  3428. goto out;
  3429. }
  3430. if (!ifp->dad_probes && addrconf_dad_end(ifp))
  3431. goto out;
  3432. write_lock_bh(&idev->lock);
  3433. if (idev->dead || !(idev->if_flags & IF_READY)) {
  3434. write_unlock_bh(&idev->lock);
  3435. goto out;
  3436. }
  3437. spin_lock(&ifp->lock);
  3438. if (ifp->state == INET6_IFADDR_STATE_DEAD) {
  3439. spin_unlock(&ifp->lock);
  3440. write_unlock_bh(&idev->lock);
  3441. goto out;
  3442. }
  3443. if (ifp->dad_probes == 0) {
  3444. bool send_na = false;
  3445. /*
  3446. * DAD was successful
  3447. */
  3448. if (ifp->flags & IFA_F_TENTATIVE &&
  3449. !(ifp->flags & IFA_F_OPTIMISTIC))
  3450. send_na = true;
  3451. bump_id = ifp->flags & IFA_F_TENTATIVE;
  3452. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  3453. spin_unlock(&ifp->lock);
  3454. write_unlock_bh(&idev->lock);
  3455. addrconf_dad_completed(ifp, bump_id, send_na);
  3456. goto out;
  3457. }
  3458. ifp->dad_probes--;
  3459. addrconf_mod_dad_work(ifp,
  3460. NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
  3461. spin_unlock(&ifp->lock);
  3462. write_unlock_bh(&idev->lock);
  3463. /* send a neighbour solicitation for our addr */
  3464. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  3465. ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
  3466. ifp->dad_nonce);
  3467. out:
  3468. in6_ifa_put(ifp);
  3469. rtnl_unlock();
  3470. }
  3471. /* ifp->idev must be at least read locked */
  3472. static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
  3473. {
  3474. struct inet6_ifaddr *ifpiter;
  3475. struct inet6_dev *idev = ifp->idev;
  3476. list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
  3477. if (ifpiter->scope > IFA_LINK)
  3478. break;
  3479. if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
  3480. (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
  3481. IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
  3482. IFA_F_PERMANENT)
  3483. return false;
  3484. }
  3485. return true;
  3486. }
  3487. static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
  3488. bool send_na)
  3489. {
  3490. struct net_device *dev = ifp->idev->dev;
  3491. struct in6_addr lladdr;
  3492. bool send_rs, send_mld;
  3493. addrconf_del_dad_work(ifp);
  3494. /*
  3495. * Configure the address for reception. Now it is valid.
  3496. */
  3497. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  3498. /* If added prefix is link local and we are prepared to process
  3499. router advertisements, start sending router solicitations.
  3500. */
  3501. read_lock_bh(&ifp->idev->lock);
  3502. send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
  3503. send_rs = send_mld &&
  3504. ipv6_accept_ra(ifp->idev) &&
  3505. ifp->idev->cnf.rtr_solicits != 0 &&
  3506. (dev->flags&IFF_LOOPBACK) == 0;
  3507. read_unlock_bh(&ifp->idev->lock);
  3508. /* While dad is in progress mld report's source address is in6_addrany.
  3509. * Resend with proper ll now.
  3510. */
  3511. if (send_mld)
  3512. ipv6_mc_dad_complete(ifp->idev);
  3513. /* send unsolicited NA if enabled */
  3514. if (send_na &&
  3515. (ifp->idev->cnf.ndisc_notify ||
  3516. dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
  3517. ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
  3518. /*router=*/ !!ifp->idev->cnf.forwarding,
  3519. /*solicited=*/ false, /*override=*/ true,
  3520. /*inc_opt=*/ true);
  3521. }
  3522. if (send_rs) {
  3523. /*
  3524. * If a host as already performed a random delay
  3525. * [...] as part of DAD [...] there is no need
  3526. * to delay again before sending the first RS
  3527. */
  3528. if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  3529. return;
  3530. ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
  3531. write_lock_bh(&ifp->idev->lock);
  3532. spin_lock(&ifp->lock);
  3533. ifp->idev->rs_interval = rfc3315_s14_backoff_init(
  3534. ifp->idev->cnf.rtr_solicit_interval);
  3535. ifp->idev->rs_probes = 1;
  3536. ifp->idev->if_flags |= IF_RS_SENT;
  3537. addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
  3538. spin_unlock(&ifp->lock);
  3539. write_unlock_bh(&ifp->idev->lock);
  3540. }
  3541. if (bump_id)
  3542. rt_genid_bump_ipv6(dev_net(dev));
  3543. /* Make sure that a new temporary address will be created
  3544. * before this temporary address becomes deprecated.
  3545. */
  3546. if (ifp->flags & IFA_F_TEMPORARY)
  3547. addrconf_verify_rtnl();
  3548. }
  3549. static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
  3550. {
  3551. struct inet6_ifaddr *ifp;
  3552. read_lock_bh(&idev->lock);
  3553. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  3554. spin_lock(&ifp->lock);
  3555. if ((ifp->flags & IFA_F_TENTATIVE &&
  3556. ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
  3557. if (restart)
  3558. ifp->state = INET6_IFADDR_STATE_PREDAD;
  3559. addrconf_dad_kick(ifp);
  3560. }
  3561. spin_unlock(&ifp->lock);
  3562. }
  3563. read_unlock_bh(&idev->lock);
  3564. }
  3565. #ifdef CONFIG_PROC_FS
  3566. struct if6_iter_state {
  3567. struct seq_net_private p;
  3568. int bucket;
  3569. int offset;
  3570. };
  3571. static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
  3572. {
  3573. struct if6_iter_state *state = seq->private;
  3574. struct net *net = seq_file_net(seq);
  3575. struct inet6_ifaddr *ifa = NULL;
  3576. int p = 0;
  3577. /* initial bucket if pos is 0 */
  3578. if (pos == 0) {
  3579. state->bucket = 0;
  3580. state->offset = 0;
  3581. }
  3582. for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
  3583. hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
  3584. addr_lst) {
  3585. if (!net_eq(dev_net(ifa->idev->dev), net))
  3586. continue;
  3587. /* sync with offset */
  3588. if (p < state->offset) {
  3589. p++;
  3590. continue;
  3591. }
  3592. return ifa;
  3593. }
  3594. /* prepare for next bucket */
  3595. state->offset = 0;
  3596. p = 0;
  3597. }
  3598. return NULL;
  3599. }
  3600. static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
  3601. struct inet6_ifaddr *ifa)
  3602. {
  3603. struct if6_iter_state *state = seq->private;
  3604. struct net *net = seq_file_net(seq);
  3605. hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
  3606. if (!net_eq(dev_net(ifa->idev->dev), net))
  3607. continue;
  3608. state->offset++;
  3609. return ifa;
  3610. }
  3611. state->offset = 0;
  3612. while (++state->bucket < IN6_ADDR_HSIZE) {
  3613. hlist_for_each_entry_rcu(ifa,
  3614. &inet6_addr_lst[state->bucket], addr_lst) {
  3615. if (!net_eq(dev_net(ifa->idev->dev), net))
  3616. continue;
  3617. return ifa;
  3618. }
  3619. }
  3620. return NULL;
  3621. }
  3622. static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
  3623. __acquires(rcu)
  3624. {
  3625. rcu_read_lock();
  3626. return if6_get_first(seq, *pos);
  3627. }
  3628. static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3629. {
  3630. struct inet6_ifaddr *ifa;
  3631. ifa = if6_get_next(seq, v);
  3632. ++*pos;
  3633. return ifa;
  3634. }
  3635. static void if6_seq_stop(struct seq_file *seq, void *v)
  3636. __releases(rcu)
  3637. {
  3638. rcu_read_unlock();
  3639. }
  3640. static int if6_seq_show(struct seq_file *seq, void *v)
  3641. {
  3642. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
  3643. seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
  3644. &ifp->addr,
  3645. ifp->idev->dev->ifindex,
  3646. ifp->prefix_len,
  3647. ifp->scope,
  3648. (u8) ifp->flags,
  3649. ifp->idev->dev->name);
  3650. return 0;
  3651. }
  3652. static const struct seq_operations if6_seq_ops = {
  3653. .start = if6_seq_start,
  3654. .next = if6_seq_next,
  3655. .show = if6_seq_show,
  3656. .stop = if6_seq_stop,
  3657. };
  3658. static int __net_init if6_proc_net_init(struct net *net)
  3659. {
  3660. if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
  3661. sizeof(struct if6_iter_state)))
  3662. return -ENOMEM;
  3663. return 0;
  3664. }
  3665. static void __net_exit if6_proc_net_exit(struct net *net)
  3666. {
  3667. remove_proc_entry("if_inet6", net->proc_net);
  3668. }
  3669. static struct pernet_operations if6_proc_net_ops = {
  3670. .init = if6_proc_net_init,
  3671. .exit = if6_proc_net_exit,
  3672. };
  3673. int __init if6_proc_init(void)
  3674. {
  3675. return register_pernet_subsys(&if6_proc_net_ops);
  3676. }
  3677. void if6_proc_exit(void)
  3678. {
  3679. unregister_pernet_subsys(&if6_proc_net_ops);
  3680. }
  3681. #endif /* CONFIG_PROC_FS */
  3682. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  3683. /* Check if address is a home address configured on any interface. */
  3684. int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
  3685. {
  3686. unsigned int hash = inet6_addr_hash(net, addr);
  3687. struct inet6_ifaddr *ifp = NULL;
  3688. int ret = 0;
  3689. rcu_read_lock();
  3690. hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
  3691. if (!net_eq(dev_net(ifp->idev->dev), net))
  3692. continue;
  3693. if (ipv6_addr_equal(&ifp->addr, addr) &&
  3694. (ifp->flags & IFA_F_HOMEADDRESS)) {
  3695. ret = 1;
  3696. break;
  3697. }
  3698. }
  3699. rcu_read_unlock();
  3700. return ret;
  3701. }
  3702. #endif
  3703. /*
  3704. * Periodic address status verification
  3705. */
  3706. static void addrconf_verify_rtnl(void)
  3707. {
  3708. unsigned long now, next, next_sec, next_sched;
  3709. struct inet6_ifaddr *ifp;
  3710. int i;
  3711. ASSERT_RTNL();
  3712. rcu_read_lock_bh();
  3713. now = jiffies;
  3714. next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
  3715. cancel_delayed_work(&addr_chk_work);
  3716. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  3717. restart:
  3718. hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
  3719. unsigned long age;
  3720. /* When setting preferred_lft to a value not zero or
  3721. * infinity, while valid_lft is infinity
  3722. * IFA_F_PERMANENT has a non-infinity life time.
  3723. */
  3724. if ((ifp->flags & IFA_F_PERMANENT) &&
  3725. (ifp->prefered_lft == INFINITY_LIFE_TIME))
  3726. continue;
  3727. spin_lock(&ifp->lock);
  3728. /* We try to batch several events at once. */
  3729. age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  3730. if (ifp->valid_lft != INFINITY_LIFE_TIME &&
  3731. age >= ifp->valid_lft) {
  3732. spin_unlock(&ifp->lock);
  3733. in6_ifa_hold(ifp);
  3734. ipv6_del_addr(ifp);
  3735. goto restart;
  3736. } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
  3737. spin_unlock(&ifp->lock);
  3738. continue;
  3739. } else if (age >= ifp->prefered_lft) {
  3740. /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
  3741. int deprecate = 0;
  3742. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  3743. deprecate = 1;
  3744. ifp->flags |= IFA_F_DEPRECATED;
  3745. }
  3746. if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
  3747. (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
  3748. next = ifp->tstamp + ifp->valid_lft * HZ;
  3749. spin_unlock(&ifp->lock);
  3750. if (deprecate) {
  3751. in6_ifa_hold(ifp);
  3752. ipv6_ifa_notify(0, ifp);
  3753. in6_ifa_put(ifp);
  3754. goto restart;
  3755. }
  3756. } else if ((ifp->flags&IFA_F_TEMPORARY) &&
  3757. !(ifp->flags&IFA_F_TENTATIVE)) {
  3758. unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
  3759. ifp->idev->cnf.dad_transmits *
  3760. NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
  3761. if (age >= ifp->prefered_lft - regen_advance) {
  3762. struct inet6_ifaddr *ifpub = ifp->ifpub;
  3763. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3764. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3765. if (!ifp->regen_count && ifpub) {
  3766. ifp->regen_count++;
  3767. in6_ifa_hold(ifp);
  3768. in6_ifa_hold(ifpub);
  3769. spin_unlock(&ifp->lock);
  3770. spin_lock(&ifpub->lock);
  3771. ifpub->regen_count = 0;
  3772. spin_unlock(&ifpub->lock);
  3773. rcu_read_unlock_bh();
  3774. ipv6_create_tempaddr(ifpub, ifp, true);
  3775. in6_ifa_put(ifpub);
  3776. in6_ifa_put(ifp);
  3777. rcu_read_lock_bh();
  3778. goto restart;
  3779. }
  3780. } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
  3781. next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
  3782. spin_unlock(&ifp->lock);
  3783. } else {
  3784. /* ifp->prefered_lft <= ifp->valid_lft */
  3785. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3786. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3787. spin_unlock(&ifp->lock);
  3788. }
  3789. }
  3790. }
  3791. next_sec = round_jiffies_up(next);
  3792. next_sched = next;
  3793. /* If rounded timeout is accurate enough, accept it. */
  3794. if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
  3795. next_sched = next_sec;
  3796. /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
  3797. if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
  3798. next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
  3799. pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
  3800. now, next, next_sec, next_sched);
  3801. mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
  3802. rcu_read_unlock_bh();
  3803. }
  3804. static void addrconf_verify_work(struct work_struct *w)
  3805. {
  3806. rtnl_lock();
  3807. addrconf_verify_rtnl();
  3808. rtnl_unlock();
  3809. }
  3810. static void addrconf_verify(void)
  3811. {
  3812. mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
  3813. }
  3814. static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
  3815. struct in6_addr **peer_pfx)
  3816. {
  3817. struct in6_addr *pfx = NULL;
  3818. *peer_pfx = NULL;
  3819. if (addr)
  3820. pfx = nla_data(addr);
  3821. if (local) {
  3822. if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
  3823. *peer_pfx = pfx;
  3824. pfx = nla_data(local);
  3825. }
  3826. return pfx;
  3827. }
  3828. static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
  3829. [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
  3830. [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
  3831. [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
  3832. [IFA_FLAGS] = { .len = sizeof(u32) },
  3833. [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
  3834. };
  3835. static int
  3836. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
  3837. struct netlink_ext_ack *extack)
  3838. {
  3839. struct net *net = sock_net(skb->sk);
  3840. struct ifaddrmsg *ifm;
  3841. struct nlattr *tb[IFA_MAX+1];
  3842. struct in6_addr *pfx, *peer_pfx;
  3843. u32 ifa_flags;
  3844. int err;
  3845. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
  3846. extack);
  3847. if (err < 0)
  3848. return err;
  3849. ifm = nlmsg_data(nlh);
  3850. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  3851. if (!pfx)
  3852. return -EINVAL;
  3853. ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
  3854. /* We ignore other flags so far. */
  3855. ifa_flags &= IFA_F_MANAGETEMPADDR;
  3856. return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
  3857. ifm->ifa_prefixlen);
  3858. }
  3859. static int modify_prefix_route(struct inet6_ifaddr *ifp,
  3860. unsigned long expires, u32 flags,
  3861. bool modify_peer)
  3862. {
  3863. struct fib6_info *f6i;
  3864. u32 prio;
  3865. f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
  3866. ifp->prefix_len,
  3867. ifp->idev->dev,
  3868. 0, RTF_GATEWAY | RTF_DEFAULT);
  3869. if (!f6i)
  3870. return -ENOENT;
  3871. prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
  3872. if (f6i->fib6_metric != prio) {
  3873. /* delete old one */
  3874. ip6_del_rt(dev_net(ifp->idev->dev), f6i);
  3875. /* add new one */
  3876. addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
  3877. ifp->prefix_len,
  3878. ifp->rt_priority, ifp->idev->dev,
  3879. expires, flags, GFP_KERNEL);
  3880. } else {
  3881. if (!expires)
  3882. fib6_clean_expires(f6i);
  3883. else
  3884. fib6_set_expires(f6i, expires);
  3885. fib6_info_release(f6i);
  3886. }
  3887. return 0;
  3888. }
  3889. static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
  3890. {
  3891. u32 flags;
  3892. clock_t expires;
  3893. unsigned long timeout;
  3894. bool was_managetempaddr;
  3895. bool had_prefixroute;
  3896. bool new_peer = false;
  3897. ASSERT_RTNL();
  3898. if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
  3899. return -EINVAL;
  3900. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
  3901. (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
  3902. return -EINVAL;
  3903. if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
  3904. cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
  3905. timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
  3906. if (addrconf_finite_timeout(timeout)) {
  3907. expires = jiffies_to_clock_t(timeout * HZ);
  3908. cfg->valid_lft = timeout;
  3909. flags = RTF_EXPIRES;
  3910. } else {
  3911. expires = 0;
  3912. flags = 0;
  3913. cfg->ifa_flags |= IFA_F_PERMANENT;
  3914. }
  3915. timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
  3916. if (addrconf_finite_timeout(timeout)) {
  3917. if (timeout == 0)
  3918. cfg->ifa_flags |= IFA_F_DEPRECATED;
  3919. cfg->preferred_lft = timeout;
  3920. }
  3921. if (cfg->peer_pfx &&
  3922. memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
  3923. if (!ipv6_addr_any(&ifp->peer_addr))
  3924. cleanup_prefix_route(ifp, expires, true, true);
  3925. new_peer = true;
  3926. }
  3927. spin_lock_bh(&ifp->lock);
  3928. was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
  3929. had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
  3930. !(ifp->flags & IFA_F_NOPREFIXROUTE);
  3931. ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
  3932. IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
  3933. IFA_F_NOPREFIXROUTE);
  3934. ifp->flags |= cfg->ifa_flags;
  3935. ifp->tstamp = jiffies;
  3936. ifp->valid_lft = cfg->valid_lft;
  3937. ifp->prefered_lft = cfg->preferred_lft;
  3938. if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
  3939. ifp->rt_priority = cfg->rt_priority;
  3940. if (new_peer)
  3941. ifp->peer_addr = *cfg->peer_pfx;
  3942. spin_unlock_bh(&ifp->lock);
  3943. if (!(ifp->flags&IFA_F_TENTATIVE))
  3944. ipv6_ifa_notify(0, ifp);
  3945. if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
  3946. int rc = -ENOENT;
  3947. if (had_prefixroute)
  3948. rc = modify_prefix_route(ifp, expires, flags, false);
  3949. /* prefix route could have been deleted; if so restore it */
  3950. if (rc == -ENOENT) {
  3951. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  3952. ifp->rt_priority, ifp->idev->dev,
  3953. expires, flags, GFP_KERNEL);
  3954. }
  3955. if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
  3956. rc = modify_prefix_route(ifp, expires, flags, true);
  3957. if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
  3958. addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
  3959. ifp->rt_priority, ifp->idev->dev,
  3960. expires, flags, GFP_KERNEL);
  3961. }
  3962. } else if (had_prefixroute) {
  3963. enum cleanup_prefix_rt_t action;
  3964. unsigned long rt_expires;
  3965. write_lock_bh(&ifp->idev->lock);
  3966. action = check_cleanup_prefix_route(ifp, &rt_expires);
  3967. write_unlock_bh(&ifp->idev->lock);
  3968. if (action != CLEANUP_PREFIX_RT_NOP) {
  3969. cleanup_prefix_route(ifp, rt_expires,
  3970. action == CLEANUP_PREFIX_RT_DEL, false);
  3971. }
  3972. }
  3973. if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
  3974. if (was_managetempaddr &&
  3975. !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
  3976. cfg->valid_lft = 0;
  3977. cfg->preferred_lft = 0;
  3978. }
  3979. manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
  3980. cfg->preferred_lft, !was_managetempaddr,
  3981. jiffies);
  3982. }
  3983. addrconf_verify_rtnl();
  3984. return 0;
  3985. }
  3986. static int
  3987. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
  3988. struct netlink_ext_ack *extack)
  3989. {
  3990. struct net *net = sock_net(skb->sk);
  3991. struct ifaddrmsg *ifm;
  3992. struct nlattr *tb[IFA_MAX+1];
  3993. struct in6_addr *peer_pfx;
  3994. struct inet6_ifaddr *ifa;
  3995. struct net_device *dev;
  3996. struct inet6_dev *idev;
  3997. struct ifa6_config cfg;
  3998. int err;
  3999. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
  4000. extack);
  4001. if (err < 0)
  4002. return err;
  4003. memset(&cfg, 0, sizeof(cfg));
  4004. ifm = nlmsg_data(nlh);
  4005. cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  4006. if (!cfg.pfx)
  4007. return -EINVAL;
  4008. cfg.peer_pfx = peer_pfx;
  4009. cfg.plen = ifm->ifa_prefixlen;
  4010. if (tb[IFA_RT_PRIORITY])
  4011. cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
  4012. cfg.valid_lft = INFINITY_LIFE_TIME;
  4013. cfg.preferred_lft = INFINITY_LIFE_TIME;
  4014. if (tb[IFA_CACHEINFO]) {
  4015. struct ifa_cacheinfo *ci;
  4016. ci = nla_data(tb[IFA_CACHEINFO]);
  4017. cfg.valid_lft = ci->ifa_valid;
  4018. cfg.preferred_lft = ci->ifa_prefered;
  4019. }
  4020. dev = __dev_get_by_index(net, ifm->ifa_index);
  4021. if (!dev)
  4022. return -ENODEV;
  4023. if (tb[IFA_FLAGS])
  4024. cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
  4025. else
  4026. cfg.ifa_flags = ifm->ifa_flags;
  4027. /* We ignore other flags so far. */
  4028. cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
  4029. IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
  4030. IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
  4031. idev = ipv6_find_idev(dev);
  4032. if (!idev)
  4033. return -ENOBUFS;
  4034. if (!ipv6_allow_optimistic_dad(net, idev))
  4035. cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
  4036. if (cfg.ifa_flags & IFA_F_NODAD &&
  4037. cfg.ifa_flags & IFA_F_OPTIMISTIC) {
  4038. NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
  4039. return -EINVAL;
  4040. }
  4041. ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
  4042. if (!ifa) {
  4043. /*
  4044. * It would be best to check for !NLM_F_CREATE here but
  4045. * userspace already relies on not having to provide this.
  4046. */
  4047. return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
  4048. }
  4049. if (nlh->nlmsg_flags & NLM_F_EXCL ||
  4050. !(nlh->nlmsg_flags & NLM_F_REPLACE))
  4051. err = -EEXIST;
  4052. else
  4053. err = inet6_addr_modify(ifa, &cfg);
  4054. in6_ifa_put(ifa);
  4055. return err;
  4056. }
  4057. static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
  4058. u8 scope, int ifindex)
  4059. {
  4060. struct ifaddrmsg *ifm;
  4061. ifm = nlmsg_data(nlh);
  4062. ifm->ifa_family = AF_INET6;
  4063. ifm->ifa_prefixlen = prefixlen;
  4064. ifm->ifa_flags = flags;
  4065. ifm->ifa_scope = scope;
  4066. ifm->ifa_index = ifindex;
  4067. }
  4068. static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
  4069. unsigned long tstamp, u32 preferred, u32 valid)
  4070. {
  4071. struct ifa_cacheinfo ci;
  4072. ci.cstamp = cstamp_delta(cstamp);
  4073. ci.tstamp = cstamp_delta(tstamp);
  4074. ci.ifa_prefered = preferred;
  4075. ci.ifa_valid = valid;
  4076. return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  4077. }
  4078. static inline int rt_scope(int ifa_scope)
  4079. {
  4080. if (ifa_scope & IFA_HOST)
  4081. return RT_SCOPE_HOST;
  4082. else if (ifa_scope & IFA_LINK)
  4083. return RT_SCOPE_LINK;
  4084. else if (ifa_scope & IFA_SITE)
  4085. return RT_SCOPE_SITE;
  4086. else
  4087. return RT_SCOPE_UNIVERSE;
  4088. }
  4089. static inline int inet6_ifaddr_msgsize(void)
  4090. {
  4091. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  4092. + nla_total_size(16) /* IFA_LOCAL */
  4093. + nla_total_size(16) /* IFA_ADDRESS */
  4094. + nla_total_size(sizeof(struct ifa_cacheinfo))
  4095. + nla_total_size(4) /* IFA_FLAGS */
  4096. + nla_total_size(4) /* IFA_RT_PRIORITY */;
  4097. }
  4098. static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
  4099. u32 portid, u32 seq, int event, unsigned int flags)
  4100. {
  4101. struct nlmsghdr *nlh;
  4102. u32 preferred, valid;
  4103. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
  4104. if (!nlh)
  4105. return -EMSGSIZE;
  4106. put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
  4107. ifa->idev->dev->ifindex);
  4108. if (!((ifa->flags&IFA_F_PERMANENT) &&
  4109. (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
  4110. preferred = ifa->prefered_lft;
  4111. valid = ifa->valid_lft;
  4112. if (preferred != INFINITY_LIFE_TIME) {
  4113. long tval = (jiffies - ifa->tstamp)/HZ;
  4114. if (preferred > tval)
  4115. preferred -= tval;
  4116. else
  4117. preferred = 0;
  4118. if (valid != INFINITY_LIFE_TIME) {
  4119. if (valid > tval)
  4120. valid -= tval;
  4121. else
  4122. valid = 0;
  4123. }
  4124. }
  4125. } else {
  4126. preferred = INFINITY_LIFE_TIME;
  4127. valid = INFINITY_LIFE_TIME;
  4128. }
  4129. if (!ipv6_addr_any(&ifa->peer_addr)) {
  4130. if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
  4131. nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
  4132. goto error;
  4133. } else
  4134. if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
  4135. goto error;
  4136. if (ifa->rt_priority &&
  4137. nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
  4138. goto error;
  4139. if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
  4140. goto error;
  4141. if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
  4142. goto error;
  4143. nlmsg_end(skb, nlh);
  4144. return 0;
  4145. error:
  4146. nlmsg_cancel(skb, nlh);
  4147. return -EMSGSIZE;
  4148. }
  4149. static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
  4150. u32 portid, u32 seq, int event, u16 flags)
  4151. {
  4152. struct nlmsghdr *nlh;
  4153. u8 scope = RT_SCOPE_UNIVERSE;
  4154. int ifindex = ifmca->idev->dev->ifindex;
  4155. if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
  4156. scope = RT_SCOPE_SITE;
  4157. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
  4158. if (!nlh)
  4159. return -EMSGSIZE;
  4160. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  4161. if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
  4162. put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
  4163. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  4164. nlmsg_cancel(skb, nlh);
  4165. return -EMSGSIZE;
  4166. }
  4167. nlmsg_end(skb, nlh);
  4168. return 0;
  4169. }
  4170. static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
  4171. u32 portid, u32 seq, int event, unsigned int flags)
  4172. {
  4173. struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
  4174. int ifindex = dev ? dev->ifindex : 1;
  4175. struct nlmsghdr *nlh;
  4176. u8 scope = RT_SCOPE_UNIVERSE;
  4177. if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
  4178. scope = RT_SCOPE_SITE;
  4179. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
  4180. if (!nlh)
  4181. return -EMSGSIZE;
  4182. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  4183. if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
  4184. put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
  4185. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  4186. nlmsg_cancel(skb, nlh);
  4187. return -EMSGSIZE;
  4188. }
  4189. nlmsg_end(skb, nlh);
  4190. return 0;
  4191. }
  4192. enum addr_type_t {
  4193. UNICAST_ADDR,
  4194. MULTICAST_ADDR,
  4195. ANYCAST_ADDR,
  4196. };
  4197. /* called with rcu_read_lock() */
  4198. static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
  4199. struct netlink_callback *cb, enum addr_type_t type,
  4200. int s_ip_idx, int *p_ip_idx)
  4201. {
  4202. struct ifmcaddr6 *ifmca;
  4203. struct ifacaddr6 *ifaca;
  4204. int err = 1;
  4205. int ip_idx = *p_ip_idx;
  4206. read_lock_bh(&idev->lock);
  4207. switch (type) {
  4208. case UNICAST_ADDR: {
  4209. struct inet6_ifaddr *ifa;
  4210. /* unicast address incl. temp addr */
  4211. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  4212. if (ip_idx < s_ip_idx)
  4213. goto next;
  4214. err = inet6_fill_ifaddr(skb, ifa,
  4215. NETLINK_CB(cb->skb).portid,
  4216. cb->nlh->nlmsg_seq,
  4217. RTM_NEWADDR,
  4218. NLM_F_MULTI);
  4219. if (err < 0)
  4220. break;
  4221. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  4222. next:
  4223. ip_idx++;
  4224. }
  4225. break;
  4226. }
  4227. case MULTICAST_ADDR:
  4228. /* multicast address */
  4229. for (ifmca = idev->mc_list; ifmca;
  4230. ifmca = ifmca->next, ip_idx++) {
  4231. if (ip_idx < s_ip_idx)
  4232. continue;
  4233. err = inet6_fill_ifmcaddr(skb, ifmca,
  4234. NETLINK_CB(cb->skb).portid,
  4235. cb->nlh->nlmsg_seq,
  4236. RTM_GETMULTICAST,
  4237. NLM_F_MULTI);
  4238. if (err < 0)
  4239. break;
  4240. }
  4241. break;
  4242. case ANYCAST_ADDR:
  4243. /* anycast address */
  4244. for (ifaca = idev->ac_list; ifaca;
  4245. ifaca = ifaca->aca_next, ip_idx++) {
  4246. if (ip_idx < s_ip_idx)
  4247. continue;
  4248. err = inet6_fill_ifacaddr(skb, ifaca,
  4249. NETLINK_CB(cb->skb).portid,
  4250. cb->nlh->nlmsg_seq,
  4251. RTM_GETANYCAST,
  4252. NLM_F_MULTI);
  4253. if (err < 0)
  4254. break;
  4255. }
  4256. break;
  4257. default:
  4258. break;
  4259. }
  4260. read_unlock_bh(&idev->lock);
  4261. *p_ip_idx = ip_idx;
  4262. return err;
  4263. }
  4264. static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
  4265. enum addr_type_t type)
  4266. {
  4267. struct net *net = sock_net(skb->sk);
  4268. int h, s_h;
  4269. int idx, ip_idx;
  4270. int s_idx, s_ip_idx;
  4271. struct net_device *dev;
  4272. struct inet6_dev *idev;
  4273. struct hlist_head *head;
  4274. s_h = cb->args[0];
  4275. s_idx = idx = cb->args[1];
  4276. s_ip_idx = ip_idx = cb->args[2];
  4277. rcu_read_lock();
  4278. cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
  4279. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  4280. idx = 0;
  4281. head = &net->dev_index_head[h];
  4282. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  4283. if (idx < s_idx)
  4284. goto cont;
  4285. if (h > s_h || idx > s_idx)
  4286. s_ip_idx = 0;
  4287. ip_idx = 0;
  4288. idev = __in6_dev_get(dev);
  4289. if (!idev)
  4290. goto cont;
  4291. if (in6_dump_addrs(idev, skb, cb, type,
  4292. s_ip_idx, &ip_idx) < 0)
  4293. goto done;
  4294. cont:
  4295. idx++;
  4296. }
  4297. }
  4298. done:
  4299. rcu_read_unlock();
  4300. cb->args[0] = h;
  4301. cb->args[1] = idx;
  4302. cb->args[2] = ip_idx;
  4303. return skb->len;
  4304. }
  4305. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4306. {
  4307. enum addr_type_t type = UNICAST_ADDR;
  4308. return inet6_dump_addr(skb, cb, type);
  4309. }
  4310. static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4311. {
  4312. enum addr_type_t type = MULTICAST_ADDR;
  4313. return inet6_dump_addr(skb, cb, type);
  4314. }
  4315. static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4316. {
  4317. enum addr_type_t type = ANYCAST_ADDR;
  4318. return inet6_dump_addr(skb, cb, type);
  4319. }
  4320. static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  4321. struct netlink_ext_ack *extack)
  4322. {
  4323. struct net *net = sock_net(in_skb->sk);
  4324. struct ifaddrmsg *ifm;
  4325. struct nlattr *tb[IFA_MAX+1];
  4326. struct in6_addr *addr = NULL, *peer;
  4327. struct net_device *dev = NULL;
  4328. struct inet6_ifaddr *ifa;
  4329. struct sk_buff *skb;
  4330. int err;
  4331. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
  4332. extack);
  4333. if (err < 0)
  4334. return err;
  4335. addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
  4336. if (!addr)
  4337. return -EINVAL;
  4338. ifm = nlmsg_data(nlh);
  4339. if (ifm->ifa_index)
  4340. dev = dev_get_by_index(net, ifm->ifa_index);
  4341. ifa = ipv6_get_ifaddr(net, addr, dev, 1);
  4342. if (!ifa) {
  4343. err = -EADDRNOTAVAIL;
  4344. goto errout;
  4345. }
  4346. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
  4347. if (!skb) {
  4348. err = -ENOBUFS;
  4349. goto errout_ifa;
  4350. }
  4351. err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
  4352. nlh->nlmsg_seq, RTM_NEWADDR, 0);
  4353. if (err < 0) {
  4354. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  4355. WARN_ON(err == -EMSGSIZE);
  4356. kfree_skb(skb);
  4357. goto errout_ifa;
  4358. }
  4359. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  4360. errout_ifa:
  4361. in6_ifa_put(ifa);
  4362. errout:
  4363. if (dev)
  4364. dev_put(dev);
  4365. return err;
  4366. }
  4367. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  4368. {
  4369. struct sk_buff *skb;
  4370. struct net *net = dev_net(ifa->idev->dev);
  4371. int err = -ENOBUFS;
  4372. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
  4373. if (!skb)
  4374. goto errout;
  4375. err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
  4376. if (err < 0) {
  4377. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  4378. WARN_ON(err == -EMSGSIZE);
  4379. kfree_skb(skb);
  4380. goto errout;
  4381. }
  4382. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
  4383. return;
  4384. errout:
  4385. if (err < 0)
  4386. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
  4387. }
  4388. static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
  4389. __s32 *array, int bytes)
  4390. {
  4391. BUG_ON(bytes < (DEVCONF_MAX * 4));
  4392. memset(array, 0, bytes);
  4393. array[DEVCONF_FORWARDING] = cnf->forwarding;
  4394. array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
  4395. array[DEVCONF_MTU6] = cnf->mtu6;
  4396. array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
  4397. array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
  4398. array[DEVCONF_AUTOCONF] = cnf->autoconf;
  4399. array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
  4400. array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
  4401. array[DEVCONF_RTR_SOLICIT_INTERVAL] =
  4402. jiffies_to_msecs(cnf->rtr_solicit_interval);
  4403. array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
  4404. jiffies_to_msecs(cnf->rtr_solicit_max_interval);
  4405. array[DEVCONF_RTR_SOLICIT_DELAY] =
  4406. jiffies_to_msecs(cnf->rtr_solicit_delay);
  4407. array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
  4408. array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
  4409. jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
  4410. array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
  4411. jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
  4412. array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
  4413. array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
  4414. array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
  4415. array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
  4416. array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
  4417. array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
  4418. array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
  4419. array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
  4420. array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
  4421. #ifdef CONFIG_IPV6_ROUTER_PREF
  4422. array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
  4423. array[DEVCONF_RTR_PROBE_INTERVAL] =
  4424. jiffies_to_msecs(cnf->rtr_probe_interval);
  4425. #ifdef CONFIG_IPV6_ROUTE_INFO
  4426. array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
  4427. array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
  4428. #endif
  4429. #endif
  4430. array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
  4431. array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
  4432. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  4433. array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
  4434. array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
  4435. #endif
  4436. #ifdef CONFIG_IPV6_MROUTE
  4437. array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
  4438. #endif
  4439. array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
  4440. array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
  4441. array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
  4442. array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
  4443. array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
  4444. array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
  4445. array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
  4446. array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
  4447. /* we omit DEVCONF_STABLE_SECRET for now */
  4448. array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
  4449. array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
  4450. array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
  4451. array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
  4452. array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
  4453. #ifdef CONFIG_IPV6_SEG6_HMAC
  4454. array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
  4455. #endif
  4456. array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
  4457. array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
  4458. array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
  4459. array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
  4460. }
  4461. static inline size_t inet6_ifla6_size(void)
  4462. {
  4463. return nla_total_size(4) /* IFLA_INET6_FLAGS */
  4464. + nla_total_size(sizeof(struct ifla_cacheinfo))
  4465. + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
  4466. + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
  4467. + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
  4468. + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
  4469. + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
  4470. + 0;
  4471. }
  4472. static inline size_t inet6_if_nlmsg_size(void)
  4473. {
  4474. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  4475. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  4476. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  4477. + nla_total_size(4) /* IFLA_MTU */
  4478. + nla_total_size(4) /* IFLA_LINK */
  4479. + nla_total_size(1) /* IFLA_OPERSTATE */
  4480. + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
  4481. }
  4482. static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
  4483. int bytes)
  4484. {
  4485. int i;
  4486. int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
  4487. BUG_ON(pad < 0);
  4488. /* Use put_unaligned() because stats may not be aligned for u64. */
  4489. put_unaligned(ICMP6_MIB_MAX, &stats[0]);
  4490. for (i = 1; i < ICMP6_MIB_MAX; i++)
  4491. put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
  4492. memset(&stats[ICMP6_MIB_MAX], 0, pad);
  4493. }
  4494. static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
  4495. int bytes, size_t syncpoff)
  4496. {
  4497. int i, c;
  4498. u64 buff[IPSTATS_MIB_MAX];
  4499. int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
  4500. BUG_ON(pad < 0);
  4501. memset(buff, 0, sizeof(buff));
  4502. buff[0] = IPSTATS_MIB_MAX;
  4503. for_each_possible_cpu(c) {
  4504. for (i = 1; i < IPSTATS_MIB_MAX; i++)
  4505. buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
  4506. }
  4507. memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
  4508. memset(&stats[IPSTATS_MIB_MAX], 0, pad);
  4509. }
  4510. static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
  4511. int bytes)
  4512. {
  4513. switch (attrtype) {
  4514. case IFLA_INET6_STATS:
  4515. __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
  4516. offsetof(struct ipstats_mib, syncp));
  4517. break;
  4518. case IFLA_INET6_ICMP6STATS:
  4519. __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
  4520. break;
  4521. }
  4522. }
  4523. static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
  4524. u32 ext_filter_mask)
  4525. {
  4526. struct nlattr *nla;
  4527. struct ifla_cacheinfo ci;
  4528. if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
  4529. goto nla_put_failure;
  4530. ci.max_reasm_len = IPV6_MAXPLEN;
  4531. ci.tstamp = cstamp_delta(idev->tstamp);
  4532. ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
  4533. ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
  4534. if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
  4535. goto nla_put_failure;
  4536. nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
  4537. if (!nla)
  4538. goto nla_put_failure;
  4539. ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
  4540. /* XXX - MC not implemented */
  4541. if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
  4542. return 0;
  4543. nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
  4544. if (!nla)
  4545. goto nla_put_failure;
  4546. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
  4547. nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
  4548. if (!nla)
  4549. goto nla_put_failure;
  4550. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
  4551. nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
  4552. if (!nla)
  4553. goto nla_put_failure;
  4554. if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
  4555. goto nla_put_failure;
  4556. read_lock_bh(&idev->lock);
  4557. memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
  4558. read_unlock_bh(&idev->lock);
  4559. return 0;
  4560. nla_put_failure:
  4561. return -EMSGSIZE;
  4562. }
  4563. static size_t inet6_get_link_af_size(const struct net_device *dev,
  4564. u32 ext_filter_mask)
  4565. {
  4566. if (!__in6_dev_get(dev))
  4567. return 0;
  4568. return inet6_ifla6_size();
  4569. }
  4570. static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
  4571. u32 ext_filter_mask)
  4572. {
  4573. struct inet6_dev *idev = __in6_dev_get(dev);
  4574. if (!idev)
  4575. return -ENODATA;
  4576. if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
  4577. return -EMSGSIZE;
  4578. return 0;
  4579. }
  4580. static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
  4581. {
  4582. struct inet6_ifaddr *ifp;
  4583. struct net_device *dev = idev->dev;
  4584. bool clear_token, update_rs = false;
  4585. struct in6_addr ll_addr;
  4586. ASSERT_RTNL();
  4587. if (!token)
  4588. return -EINVAL;
  4589. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
  4590. return -EINVAL;
  4591. if (!ipv6_accept_ra(idev))
  4592. return -EINVAL;
  4593. if (idev->cnf.rtr_solicits == 0)
  4594. return -EINVAL;
  4595. write_lock_bh(&idev->lock);
  4596. BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
  4597. memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
  4598. write_unlock_bh(&idev->lock);
  4599. clear_token = ipv6_addr_any(token);
  4600. if (clear_token)
  4601. goto update_lft;
  4602. if (!idev->dead && (idev->if_flags & IF_READY) &&
  4603. !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
  4604. IFA_F_OPTIMISTIC)) {
  4605. /* If we're not ready, then normal ifup will take care
  4606. * of this. Otherwise, we need to request our rs here.
  4607. */
  4608. ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
  4609. update_rs = true;
  4610. }
  4611. update_lft:
  4612. write_lock_bh(&idev->lock);
  4613. if (update_rs) {
  4614. idev->if_flags |= IF_RS_SENT;
  4615. idev->rs_interval = rfc3315_s14_backoff_init(
  4616. idev->cnf.rtr_solicit_interval);
  4617. idev->rs_probes = 1;
  4618. addrconf_mod_rs_timer(idev, idev->rs_interval);
  4619. }
  4620. /* Well, that's kinda nasty ... */
  4621. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  4622. spin_lock(&ifp->lock);
  4623. if (ifp->tokenized) {
  4624. ifp->valid_lft = 0;
  4625. ifp->prefered_lft = 0;
  4626. }
  4627. spin_unlock(&ifp->lock);
  4628. }
  4629. write_unlock_bh(&idev->lock);
  4630. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  4631. addrconf_verify_rtnl();
  4632. return 0;
  4633. }
  4634. static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
  4635. [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
  4636. [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
  4637. };
  4638. static int inet6_validate_link_af(const struct net_device *dev,
  4639. const struct nlattr *nla)
  4640. {
  4641. struct nlattr *tb[IFLA_INET6_MAX + 1];
  4642. if (dev && !__in6_dev_get(dev))
  4643. return -EAFNOSUPPORT;
  4644. return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
  4645. NULL);
  4646. }
  4647. static int check_addr_gen_mode(int mode)
  4648. {
  4649. if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
  4650. mode != IN6_ADDR_GEN_MODE_NONE &&
  4651. mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
  4652. mode != IN6_ADDR_GEN_MODE_RANDOM)
  4653. return -EINVAL;
  4654. return 1;
  4655. }
  4656. static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
  4657. int mode)
  4658. {
  4659. if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
  4660. !idev->cnf.stable_secret.initialized &&
  4661. !net->ipv6.devconf_dflt->stable_secret.initialized)
  4662. return -EINVAL;
  4663. return 1;
  4664. }
  4665. static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
  4666. {
  4667. int err = -EINVAL;
  4668. struct inet6_dev *idev = __in6_dev_get(dev);
  4669. struct nlattr *tb[IFLA_INET6_MAX + 1];
  4670. if (!idev)
  4671. return -EAFNOSUPPORT;
  4672. if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
  4673. BUG();
  4674. if (tb[IFLA_INET6_TOKEN]) {
  4675. err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
  4676. if (err)
  4677. return err;
  4678. }
  4679. if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
  4680. u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
  4681. if (check_addr_gen_mode(mode) < 0 ||
  4682. check_stable_privacy(idev, dev_net(dev), mode) < 0)
  4683. return -EINVAL;
  4684. idev->cnf.addr_gen_mode = mode;
  4685. err = 0;
  4686. }
  4687. return err;
  4688. }
  4689. static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
  4690. u32 portid, u32 seq, int event, unsigned int flags)
  4691. {
  4692. struct net_device *dev = idev->dev;
  4693. struct ifinfomsg *hdr;
  4694. struct nlmsghdr *nlh;
  4695. void *protoinfo;
  4696. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
  4697. if (!nlh)
  4698. return -EMSGSIZE;
  4699. hdr = nlmsg_data(nlh);
  4700. hdr->ifi_family = AF_INET6;
  4701. hdr->__ifi_pad = 0;
  4702. hdr->ifi_type = dev->type;
  4703. hdr->ifi_index = dev->ifindex;
  4704. hdr->ifi_flags = dev_get_flags(dev);
  4705. hdr->ifi_change = 0;
  4706. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  4707. (dev->addr_len &&
  4708. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  4709. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  4710. (dev->ifindex != dev_get_iflink(dev) &&
  4711. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
  4712. nla_put_u8(skb, IFLA_OPERSTATE,
  4713. netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
  4714. goto nla_put_failure;
  4715. protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
  4716. if (!protoinfo)
  4717. goto nla_put_failure;
  4718. if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
  4719. goto nla_put_failure;
  4720. nla_nest_end(skb, protoinfo);
  4721. nlmsg_end(skb, nlh);
  4722. return 0;
  4723. nla_put_failure:
  4724. nlmsg_cancel(skb, nlh);
  4725. return -EMSGSIZE;
  4726. }
  4727. static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  4728. {
  4729. struct net *net = sock_net(skb->sk);
  4730. int h, s_h;
  4731. int idx = 0, s_idx;
  4732. struct net_device *dev;
  4733. struct inet6_dev *idev;
  4734. struct hlist_head *head;
  4735. s_h = cb->args[0];
  4736. s_idx = cb->args[1];
  4737. rcu_read_lock();
  4738. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  4739. idx = 0;
  4740. head = &net->dev_index_head[h];
  4741. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  4742. if (idx < s_idx)
  4743. goto cont;
  4744. idev = __in6_dev_get(dev);
  4745. if (!idev)
  4746. goto cont;
  4747. if (inet6_fill_ifinfo(skb, idev,
  4748. NETLINK_CB(cb->skb).portid,
  4749. cb->nlh->nlmsg_seq,
  4750. RTM_NEWLINK, NLM_F_MULTI) < 0)
  4751. goto out;
  4752. cont:
  4753. idx++;
  4754. }
  4755. }
  4756. out:
  4757. rcu_read_unlock();
  4758. cb->args[1] = idx;
  4759. cb->args[0] = h;
  4760. return skb->len;
  4761. }
  4762. void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
  4763. {
  4764. struct sk_buff *skb;
  4765. struct net *net = dev_net(idev->dev);
  4766. int err = -ENOBUFS;
  4767. skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
  4768. if (!skb)
  4769. goto errout;
  4770. err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
  4771. if (err < 0) {
  4772. /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
  4773. WARN_ON(err == -EMSGSIZE);
  4774. kfree_skb(skb);
  4775. goto errout;
  4776. }
  4777. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
  4778. return;
  4779. errout:
  4780. if (err < 0)
  4781. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
  4782. }
  4783. static inline size_t inet6_prefix_nlmsg_size(void)
  4784. {
  4785. return NLMSG_ALIGN(sizeof(struct prefixmsg))
  4786. + nla_total_size(sizeof(struct in6_addr))
  4787. + nla_total_size(sizeof(struct prefix_cacheinfo));
  4788. }
  4789. static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
  4790. struct prefix_info *pinfo, u32 portid, u32 seq,
  4791. int event, unsigned int flags)
  4792. {
  4793. struct prefixmsg *pmsg;
  4794. struct nlmsghdr *nlh;
  4795. struct prefix_cacheinfo ci;
  4796. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
  4797. if (!nlh)
  4798. return -EMSGSIZE;
  4799. pmsg = nlmsg_data(nlh);
  4800. pmsg->prefix_family = AF_INET6;
  4801. pmsg->prefix_pad1 = 0;
  4802. pmsg->prefix_pad2 = 0;
  4803. pmsg->prefix_ifindex = idev->dev->ifindex;
  4804. pmsg->prefix_len = pinfo->prefix_len;
  4805. pmsg->prefix_type = pinfo->type;
  4806. pmsg->prefix_pad3 = 0;
  4807. pmsg->prefix_flags = 0;
  4808. if (pinfo->onlink)
  4809. pmsg->prefix_flags |= IF_PREFIX_ONLINK;
  4810. if (pinfo->autoconf)
  4811. pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
  4812. if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
  4813. goto nla_put_failure;
  4814. ci.preferred_time = ntohl(pinfo->prefered);
  4815. ci.valid_time = ntohl(pinfo->valid);
  4816. if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
  4817. goto nla_put_failure;
  4818. nlmsg_end(skb, nlh);
  4819. return 0;
  4820. nla_put_failure:
  4821. nlmsg_cancel(skb, nlh);
  4822. return -EMSGSIZE;
  4823. }
  4824. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  4825. struct prefix_info *pinfo)
  4826. {
  4827. struct sk_buff *skb;
  4828. struct net *net = dev_net(idev->dev);
  4829. int err = -ENOBUFS;
  4830. skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
  4831. if (!skb)
  4832. goto errout;
  4833. err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
  4834. if (err < 0) {
  4835. /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
  4836. WARN_ON(err == -EMSGSIZE);
  4837. kfree_skb(skb);
  4838. goto errout;
  4839. }
  4840. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
  4841. return;
  4842. errout:
  4843. if (err < 0)
  4844. rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
  4845. }
  4846. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  4847. {
  4848. struct net *net = dev_net(ifp->idev->dev);
  4849. if (event)
  4850. ASSERT_RTNL();
  4851. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  4852. switch (event) {
  4853. case RTM_NEWADDR:
  4854. /*
  4855. * If the address was optimistic we inserted the route at the
  4856. * start of our DAD process, so we don't need to do it again.
  4857. * If the device was taken down in the middle of the DAD
  4858. * cycle there is a race where we could get here without a
  4859. * host route, so nothing to insert. That will be fixed when
  4860. * the device is brought up.
  4861. */
  4862. if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
  4863. ip6_ins_rt(net, ifp->rt);
  4864. } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
  4865. pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
  4866. &ifp->addr, ifp->idev->dev->name);
  4867. }
  4868. if (ifp->idev->cnf.forwarding)
  4869. addrconf_join_anycast(ifp);
  4870. if (!ipv6_addr_any(&ifp->peer_addr))
  4871. addrconf_prefix_route(&ifp->peer_addr, 128,
  4872. ifp->rt_priority, ifp->idev->dev,
  4873. 0, 0, GFP_ATOMIC);
  4874. break;
  4875. case RTM_DELADDR:
  4876. if (ifp->idev->cnf.forwarding)
  4877. addrconf_leave_anycast(ifp);
  4878. addrconf_leave_solict(ifp->idev, &ifp->addr);
  4879. if (!ipv6_addr_any(&ifp->peer_addr)) {
  4880. struct fib6_info *rt;
  4881. rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
  4882. ifp->idev->dev, 0, 0);
  4883. if (rt)
  4884. ip6_del_rt(net, rt);
  4885. }
  4886. if (ifp->rt) {
  4887. ip6_del_rt(net, ifp->rt);
  4888. ifp->rt = NULL;
  4889. }
  4890. rt_genid_bump_ipv6(net);
  4891. break;
  4892. }
  4893. atomic_inc(&net->ipv6.dev_addr_genid);
  4894. }
  4895. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  4896. {
  4897. rcu_read_lock_bh();
  4898. if (likely(ifp->idev->dead == 0))
  4899. __ipv6_ifa_notify(event, ifp);
  4900. rcu_read_unlock_bh();
  4901. }
  4902. #ifdef CONFIG_SYSCTL
  4903. static
  4904. int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
  4905. void __user *buffer, size_t *lenp, loff_t *ppos)
  4906. {
  4907. int *valp = ctl->data;
  4908. int val = *valp;
  4909. loff_t pos = *ppos;
  4910. struct ctl_table lctl;
  4911. int ret;
  4912. /*
  4913. * ctl->data points to idev->cnf.forwarding, we should
  4914. * not modify it until we get the rtnl lock.
  4915. */
  4916. lctl = *ctl;
  4917. lctl.data = &val;
  4918. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  4919. if (write)
  4920. ret = addrconf_fixup_forwarding(ctl, valp, val);
  4921. if (ret)
  4922. *ppos = pos;
  4923. return ret;
  4924. }
  4925. static
  4926. int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
  4927. void __user *buffer, size_t *lenp, loff_t *ppos)
  4928. {
  4929. struct inet6_dev *idev = ctl->extra1;
  4930. int min_mtu = IPV6_MIN_MTU;
  4931. struct ctl_table lctl;
  4932. lctl = *ctl;
  4933. lctl.extra1 = &min_mtu;
  4934. lctl.extra2 = idev ? &idev->dev->mtu : NULL;
  4935. return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
  4936. }
  4937. static void dev_disable_change(struct inet6_dev *idev)
  4938. {
  4939. struct netdev_notifier_info info;
  4940. if (!idev || !idev->dev)
  4941. return;
  4942. netdev_notifier_info_init(&info, idev->dev);
  4943. if (idev->cnf.disable_ipv6)
  4944. addrconf_notify(NULL, NETDEV_DOWN, &info);
  4945. else
  4946. addrconf_notify(NULL, NETDEV_UP, &info);
  4947. }
  4948. static void addrconf_disable_change(struct net *net, __s32 newf)
  4949. {
  4950. struct net_device *dev;
  4951. struct inet6_dev *idev;
  4952. for_each_netdev(net, dev) {
  4953. idev = __in6_dev_get(dev);
  4954. if (idev) {
  4955. int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
  4956. idev->cnf.disable_ipv6 = newf;
  4957. if (changed)
  4958. dev_disable_change(idev);
  4959. }
  4960. }
  4961. }
  4962. static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
  4963. {
  4964. struct net *net;
  4965. int old;
  4966. if (!rtnl_trylock())
  4967. return restart_syscall();
  4968. net = (struct net *)table->extra2;
  4969. old = *p;
  4970. *p = newf;
  4971. if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
  4972. rtnl_unlock();
  4973. return 0;
  4974. }
  4975. if (p == &net->ipv6.devconf_all->disable_ipv6) {
  4976. net->ipv6.devconf_dflt->disable_ipv6 = newf;
  4977. addrconf_disable_change(net, newf);
  4978. } else if ((!newf) ^ (!old))
  4979. dev_disable_change((struct inet6_dev *)table->extra1);
  4980. rtnl_unlock();
  4981. return 0;
  4982. }
  4983. static
  4984. int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
  4985. void __user *buffer, size_t *lenp, loff_t *ppos)
  4986. {
  4987. int *valp = ctl->data;
  4988. int val = *valp;
  4989. loff_t pos = *ppos;
  4990. struct ctl_table lctl;
  4991. int ret;
  4992. /*
  4993. * ctl->data points to idev->cnf.disable_ipv6, we should
  4994. * not modify it until we get the rtnl lock.
  4995. */
  4996. lctl = *ctl;
  4997. lctl.data = &val;
  4998. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  4999. if (write)
  5000. ret = addrconf_disable_ipv6(ctl, valp, val);
  5001. if (ret)
  5002. *ppos = pos;
  5003. return ret;
  5004. }
  5005. static
  5006. int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
  5007. void __user *buffer, size_t *lenp, loff_t *ppos)
  5008. {
  5009. int *valp = ctl->data;
  5010. int ret;
  5011. int old, new;
  5012. old = *valp;
  5013. ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  5014. new = *valp;
  5015. if (write && old != new) {
  5016. struct net *net = ctl->extra2;
  5017. if (!rtnl_trylock())
  5018. return restart_syscall();
  5019. if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
  5020. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5021. NETCONFA_PROXY_NEIGH,
  5022. NETCONFA_IFINDEX_DEFAULT,
  5023. net->ipv6.devconf_dflt);
  5024. else if (valp == &net->ipv6.devconf_all->proxy_ndp)
  5025. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5026. NETCONFA_PROXY_NEIGH,
  5027. NETCONFA_IFINDEX_ALL,
  5028. net->ipv6.devconf_all);
  5029. else {
  5030. struct inet6_dev *idev = ctl->extra1;
  5031. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5032. NETCONFA_PROXY_NEIGH,
  5033. idev->dev->ifindex,
  5034. &idev->cnf);
  5035. }
  5036. rtnl_unlock();
  5037. }
  5038. return ret;
  5039. }
  5040. static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
  5041. void __user *buffer, size_t *lenp,
  5042. loff_t *ppos)
  5043. {
  5044. int ret = 0;
  5045. u32 new_val;
  5046. struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
  5047. struct net *net = (struct net *)ctl->extra2;
  5048. struct ctl_table tmp = {
  5049. .data = &new_val,
  5050. .maxlen = sizeof(new_val),
  5051. .mode = ctl->mode,
  5052. };
  5053. if (!rtnl_trylock())
  5054. return restart_syscall();
  5055. new_val = *((u32 *)ctl->data);
  5056. ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
  5057. if (ret != 0)
  5058. goto out;
  5059. if (write) {
  5060. if (check_addr_gen_mode(new_val) < 0) {
  5061. ret = -EINVAL;
  5062. goto out;
  5063. }
  5064. if (idev) {
  5065. if (check_stable_privacy(idev, net, new_val) < 0) {
  5066. ret = -EINVAL;
  5067. goto out;
  5068. }
  5069. if (idev->cnf.addr_gen_mode != new_val) {
  5070. idev->cnf.addr_gen_mode = new_val;
  5071. addrconf_dev_config(idev->dev);
  5072. }
  5073. } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
  5074. struct net_device *dev;
  5075. net->ipv6.devconf_dflt->addr_gen_mode = new_val;
  5076. for_each_netdev(net, dev) {
  5077. idev = __in6_dev_get(dev);
  5078. if (idev &&
  5079. idev->cnf.addr_gen_mode != new_val) {
  5080. idev->cnf.addr_gen_mode = new_val;
  5081. addrconf_dev_config(idev->dev);
  5082. }
  5083. }
  5084. }
  5085. *((u32 *)ctl->data) = new_val;
  5086. }
  5087. out:
  5088. rtnl_unlock();
  5089. return ret;
  5090. }
  5091. static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
  5092. void __user *buffer, size_t *lenp,
  5093. loff_t *ppos)
  5094. {
  5095. int err;
  5096. struct in6_addr addr;
  5097. char str[IPV6_MAX_STRLEN];
  5098. struct ctl_table lctl = *ctl;
  5099. struct net *net = ctl->extra2;
  5100. struct ipv6_stable_secret *secret = ctl->data;
  5101. if (&net->ipv6.devconf_all->stable_secret == ctl->data)
  5102. return -EIO;
  5103. lctl.maxlen = IPV6_MAX_STRLEN;
  5104. lctl.data = str;
  5105. if (!rtnl_trylock())
  5106. return restart_syscall();
  5107. if (!write && !secret->initialized) {
  5108. err = -EIO;
  5109. goto out;
  5110. }
  5111. err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
  5112. if (err >= sizeof(str)) {
  5113. err = -EIO;
  5114. goto out;
  5115. }
  5116. err = proc_dostring(&lctl, write, buffer, lenp, ppos);
  5117. if (err || !write)
  5118. goto out;
  5119. if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
  5120. err = -EIO;
  5121. goto out;
  5122. }
  5123. secret->initialized = true;
  5124. secret->secret = addr;
  5125. if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
  5126. struct net_device *dev;
  5127. for_each_netdev(net, dev) {
  5128. struct inet6_dev *idev = __in6_dev_get(dev);
  5129. if (idev) {
  5130. idev->cnf.addr_gen_mode =
  5131. IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
  5132. }
  5133. }
  5134. } else {
  5135. struct inet6_dev *idev = ctl->extra1;
  5136. idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
  5137. }
  5138. out:
  5139. rtnl_unlock();
  5140. return err;
  5141. }
  5142. static
  5143. int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
  5144. int write,
  5145. void __user *buffer,
  5146. size_t *lenp,
  5147. loff_t *ppos)
  5148. {
  5149. int *valp = ctl->data;
  5150. int val = *valp;
  5151. loff_t pos = *ppos;
  5152. struct ctl_table lctl;
  5153. int ret;
  5154. /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
  5155. * we should not modify it until we get the rtnl lock.
  5156. */
  5157. lctl = *ctl;
  5158. lctl.data = &val;
  5159. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5160. if (write)
  5161. ret = addrconf_fixup_linkdown(ctl, valp, val);
  5162. if (ret)
  5163. *ppos = pos;
  5164. return ret;
  5165. }
  5166. static
  5167. void addrconf_set_nopolicy(struct rt6_info *rt, int action)
  5168. {
  5169. if (rt) {
  5170. if (action)
  5171. rt->dst.flags |= DST_NOPOLICY;
  5172. else
  5173. rt->dst.flags &= ~DST_NOPOLICY;
  5174. }
  5175. }
  5176. static
  5177. void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
  5178. {
  5179. struct inet6_ifaddr *ifa;
  5180. read_lock_bh(&idev->lock);
  5181. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  5182. spin_lock(&ifa->lock);
  5183. if (ifa->rt) {
  5184. struct fib6_info *rt = ifa->rt;
  5185. int cpu;
  5186. rcu_read_lock();
  5187. ifa->rt->dst_nopolicy = val ? true : false;
  5188. if (rt->rt6i_pcpu) {
  5189. for_each_possible_cpu(cpu) {
  5190. struct rt6_info **rtp;
  5191. rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
  5192. addrconf_set_nopolicy(*rtp, val);
  5193. }
  5194. }
  5195. rcu_read_unlock();
  5196. }
  5197. spin_unlock(&ifa->lock);
  5198. }
  5199. read_unlock_bh(&idev->lock);
  5200. }
  5201. static
  5202. int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
  5203. {
  5204. struct inet6_dev *idev;
  5205. struct net *net;
  5206. if (!rtnl_trylock())
  5207. return restart_syscall();
  5208. *valp = val;
  5209. net = (struct net *)ctl->extra2;
  5210. if (valp == &net->ipv6.devconf_dflt->disable_policy) {
  5211. rtnl_unlock();
  5212. return 0;
  5213. }
  5214. if (valp == &net->ipv6.devconf_all->disable_policy) {
  5215. struct net_device *dev;
  5216. for_each_netdev(net, dev) {
  5217. idev = __in6_dev_get(dev);
  5218. if (idev)
  5219. addrconf_disable_policy_idev(idev, val);
  5220. }
  5221. } else {
  5222. idev = (struct inet6_dev *)ctl->extra1;
  5223. addrconf_disable_policy_idev(idev, val);
  5224. }
  5225. rtnl_unlock();
  5226. return 0;
  5227. }
  5228. static
  5229. int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
  5230. void __user *buffer, size_t *lenp,
  5231. loff_t *ppos)
  5232. {
  5233. int *valp = ctl->data;
  5234. int val = *valp;
  5235. loff_t pos = *ppos;
  5236. struct ctl_table lctl;
  5237. int ret;
  5238. lctl = *ctl;
  5239. lctl.data = &val;
  5240. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5241. if (write && (*valp != val))
  5242. ret = addrconf_disable_policy(ctl, valp, val);
  5243. if (ret)
  5244. *ppos = pos;
  5245. return ret;
  5246. }
  5247. static int minus_one = -1;
  5248. static const int zero = 0;
  5249. static const int one = 1;
  5250. static const int two_five_five = 255;
  5251. static const struct ctl_table addrconf_sysctl[] = {
  5252. {
  5253. .procname = "forwarding",
  5254. .data = &ipv6_devconf.forwarding,
  5255. .maxlen = sizeof(int),
  5256. .mode = 0644,
  5257. .proc_handler = addrconf_sysctl_forward,
  5258. },
  5259. {
  5260. .procname = "hop_limit",
  5261. .data = &ipv6_devconf.hop_limit,
  5262. .maxlen = sizeof(int),
  5263. .mode = 0644,
  5264. .proc_handler = proc_dointvec_minmax,
  5265. .extra1 = (void *)&one,
  5266. .extra2 = (void *)&two_five_five,
  5267. },
  5268. {
  5269. .procname = "mtu",
  5270. .data = &ipv6_devconf.mtu6,
  5271. .maxlen = sizeof(int),
  5272. .mode = 0644,
  5273. .proc_handler = addrconf_sysctl_mtu,
  5274. },
  5275. {
  5276. .procname = "accept_ra",
  5277. .data = &ipv6_devconf.accept_ra,
  5278. .maxlen = sizeof(int),
  5279. .mode = 0644,
  5280. .proc_handler = proc_dointvec,
  5281. },
  5282. {
  5283. .procname = "accept_redirects",
  5284. .data = &ipv6_devconf.accept_redirects,
  5285. .maxlen = sizeof(int),
  5286. .mode = 0644,
  5287. .proc_handler = proc_dointvec,
  5288. },
  5289. {
  5290. .procname = "autoconf",
  5291. .data = &ipv6_devconf.autoconf,
  5292. .maxlen = sizeof(int),
  5293. .mode = 0644,
  5294. .proc_handler = proc_dointvec,
  5295. },
  5296. {
  5297. .procname = "dad_transmits",
  5298. .data = &ipv6_devconf.dad_transmits,
  5299. .maxlen = sizeof(int),
  5300. .mode = 0644,
  5301. .proc_handler = proc_dointvec,
  5302. },
  5303. {
  5304. .procname = "router_solicitations",
  5305. .data = &ipv6_devconf.rtr_solicits,
  5306. .maxlen = sizeof(int),
  5307. .mode = 0644,
  5308. .proc_handler = proc_dointvec_minmax,
  5309. .extra1 = &minus_one,
  5310. },
  5311. {
  5312. .procname = "router_solicitation_interval",
  5313. .data = &ipv6_devconf.rtr_solicit_interval,
  5314. .maxlen = sizeof(int),
  5315. .mode = 0644,
  5316. .proc_handler = proc_dointvec_jiffies,
  5317. },
  5318. {
  5319. .procname = "router_solicitation_max_interval",
  5320. .data = &ipv6_devconf.rtr_solicit_max_interval,
  5321. .maxlen = sizeof(int),
  5322. .mode = 0644,
  5323. .proc_handler = proc_dointvec_jiffies,
  5324. },
  5325. {
  5326. .procname = "router_solicitation_delay",
  5327. .data = &ipv6_devconf.rtr_solicit_delay,
  5328. .maxlen = sizeof(int),
  5329. .mode = 0644,
  5330. .proc_handler = proc_dointvec_jiffies,
  5331. },
  5332. {
  5333. .procname = "force_mld_version",
  5334. .data = &ipv6_devconf.force_mld_version,
  5335. .maxlen = sizeof(int),
  5336. .mode = 0644,
  5337. .proc_handler = proc_dointvec,
  5338. },
  5339. {
  5340. .procname = "mldv1_unsolicited_report_interval",
  5341. .data =
  5342. &ipv6_devconf.mldv1_unsolicited_report_interval,
  5343. .maxlen = sizeof(int),
  5344. .mode = 0644,
  5345. .proc_handler = proc_dointvec_ms_jiffies,
  5346. },
  5347. {
  5348. .procname = "mldv2_unsolicited_report_interval",
  5349. .data =
  5350. &ipv6_devconf.mldv2_unsolicited_report_interval,
  5351. .maxlen = sizeof(int),
  5352. .mode = 0644,
  5353. .proc_handler = proc_dointvec_ms_jiffies,
  5354. },
  5355. {
  5356. .procname = "use_tempaddr",
  5357. .data = &ipv6_devconf.use_tempaddr,
  5358. .maxlen = sizeof(int),
  5359. .mode = 0644,
  5360. .proc_handler = proc_dointvec,
  5361. },
  5362. {
  5363. .procname = "temp_valid_lft",
  5364. .data = &ipv6_devconf.temp_valid_lft,
  5365. .maxlen = sizeof(int),
  5366. .mode = 0644,
  5367. .proc_handler = proc_dointvec,
  5368. },
  5369. {
  5370. .procname = "temp_prefered_lft",
  5371. .data = &ipv6_devconf.temp_prefered_lft,
  5372. .maxlen = sizeof(int),
  5373. .mode = 0644,
  5374. .proc_handler = proc_dointvec,
  5375. },
  5376. {
  5377. .procname = "regen_max_retry",
  5378. .data = &ipv6_devconf.regen_max_retry,
  5379. .maxlen = sizeof(int),
  5380. .mode = 0644,
  5381. .proc_handler = proc_dointvec,
  5382. },
  5383. {
  5384. .procname = "max_desync_factor",
  5385. .data = &ipv6_devconf.max_desync_factor,
  5386. .maxlen = sizeof(int),
  5387. .mode = 0644,
  5388. .proc_handler = proc_dointvec,
  5389. },
  5390. {
  5391. .procname = "max_addresses",
  5392. .data = &ipv6_devconf.max_addresses,
  5393. .maxlen = sizeof(int),
  5394. .mode = 0644,
  5395. .proc_handler = proc_dointvec,
  5396. },
  5397. {
  5398. .procname = "accept_ra_defrtr",
  5399. .data = &ipv6_devconf.accept_ra_defrtr,
  5400. .maxlen = sizeof(int),
  5401. .mode = 0644,
  5402. .proc_handler = proc_dointvec,
  5403. },
  5404. {
  5405. .procname = "accept_ra_min_hop_limit",
  5406. .data = &ipv6_devconf.accept_ra_min_hop_limit,
  5407. .maxlen = sizeof(int),
  5408. .mode = 0644,
  5409. .proc_handler = proc_dointvec,
  5410. },
  5411. {
  5412. .procname = "accept_ra_pinfo",
  5413. .data = &ipv6_devconf.accept_ra_pinfo,
  5414. .maxlen = sizeof(int),
  5415. .mode = 0644,
  5416. .proc_handler = proc_dointvec,
  5417. },
  5418. #ifdef CONFIG_IPV6_ROUTER_PREF
  5419. {
  5420. .procname = "accept_ra_rtr_pref",
  5421. .data = &ipv6_devconf.accept_ra_rtr_pref,
  5422. .maxlen = sizeof(int),
  5423. .mode = 0644,
  5424. .proc_handler = proc_dointvec,
  5425. },
  5426. {
  5427. .procname = "router_probe_interval",
  5428. .data = &ipv6_devconf.rtr_probe_interval,
  5429. .maxlen = sizeof(int),
  5430. .mode = 0644,
  5431. .proc_handler = proc_dointvec_jiffies,
  5432. },
  5433. #ifdef CONFIG_IPV6_ROUTE_INFO
  5434. {
  5435. .procname = "accept_ra_rt_info_min_plen",
  5436. .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
  5437. .maxlen = sizeof(int),
  5438. .mode = 0644,
  5439. .proc_handler = proc_dointvec,
  5440. },
  5441. {
  5442. .procname = "accept_ra_rt_info_max_plen",
  5443. .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
  5444. .maxlen = sizeof(int),
  5445. .mode = 0644,
  5446. .proc_handler = proc_dointvec,
  5447. },
  5448. #endif
  5449. #endif
  5450. {
  5451. .procname = "proxy_ndp",
  5452. .data = &ipv6_devconf.proxy_ndp,
  5453. .maxlen = sizeof(int),
  5454. .mode = 0644,
  5455. .proc_handler = addrconf_sysctl_proxy_ndp,
  5456. },
  5457. {
  5458. .procname = "accept_source_route",
  5459. .data = &ipv6_devconf.accept_source_route,
  5460. .maxlen = sizeof(int),
  5461. .mode = 0644,
  5462. .proc_handler = proc_dointvec,
  5463. },
  5464. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  5465. {
  5466. .procname = "optimistic_dad",
  5467. .data = &ipv6_devconf.optimistic_dad,
  5468. .maxlen = sizeof(int),
  5469. .mode = 0644,
  5470. .proc_handler = proc_dointvec,
  5471. },
  5472. {
  5473. .procname = "use_optimistic",
  5474. .data = &ipv6_devconf.use_optimistic,
  5475. .maxlen = sizeof(int),
  5476. .mode = 0644,
  5477. .proc_handler = proc_dointvec,
  5478. },
  5479. #endif
  5480. #ifdef CONFIG_IPV6_MROUTE
  5481. {
  5482. .procname = "mc_forwarding",
  5483. .data = &ipv6_devconf.mc_forwarding,
  5484. .maxlen = sizeof(int),
  5485. .mode = 0444,
  5486. .proc_handler = proc_dointvec,
  5487. },
  5488. #endif
  5489. {
  5490. .procname = "disable_ipv6",
  5491. .data = &ipv6_devconf.disable_ipv6,
  5492. .maxlen = sizeof(int),
  5493. .mode = 0644,
  5494. .proc_handler = addrconf_sysctl_disable,
  5495. },
  5496. {
  5497. .procname = "accept_dad",
  5498. .data = &ipv6_devconf.accept_dad,
  5499. .maxlen = sizeof(int),
  5500. .mode = 0644,
  5501. .proc_handler = proc_dointvec,
  5502. },
  5503. {
  5504. .procname = "force_tllao",
  5505. .data = &ipv6_devconf.force_tllao,
  5506. .maxlen = sizeof(int),
  5507. .mode = 0644,
  5508. .proc_handler = proc_dointvec
  5509. },
  5510. {
  5511. .procname = "ndisc_notify",
  5512. .data = &ipv6_devconf.ndisc_notify,
  5513. .maxlen = sizeof(int),
  5514. .mode = 0644,
  5515. .proc_handler = proc_dointvec
  5516. },
  5517. {
  5518. .procname = "suppress_frag_ndisc",
  5519. .data = &ipv6_devconf.suppress_frag_ndisc,
  5520. .maxlen = sizeof(int),
  5521. .mode = 0644,
  5522. .proc_handler = proc_dointvec
  5523. },
  5524. {
  5525. .procname = "accept_ra_from_local",
  5526. .data = &ipv6_devconf.accept_ra_from_local,
  5527. .maxlen = sizeof(int),
  5528. .mode = 0644,
  5529. .proc_handler = proc_dointvec,
  5530. },
  5531. {
  5532. .procname = "accept_ra_mtu",
  5533. .data = &ipv6_devconf.accept_ra_mtu,
  5534. .maxlen = sizeof(int),
  5535. .mode = 0644,
  5536. .proc_handler = proc_dointvec,
  5537. },
  5538. {
  5539. .procname = "stable_secret",
  5540. .data = &ipv6_devconf.stable_secret,
  5541. .maxlen = IPV6_MAX_STRLEN,
  5542. .mode = 0600,
  5543. .proc_handler = addrconf_sysctl_stable_secret,
  5544. },
  5545. {
  5546. .procname = "use_oif_addrs_only",
  5547. .data = &ipv6_devconf.use_oif_addrs_only,
  5548. .maxlen = sizeof(int),
  5549. .mode = 0644,
  5550. .proc_handler = proc_dointvec,
  5551. },
  5552. {
  5553. .procname = "ignore_routes_with_linkdown",
  5554. .data = &ipv6_devconf.ignore_routes_with_linkdown,
  5555. .maxlen = sizeof(int),
  5556. .mode = 0644,
  5557. .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
  5558. },
  5559. {
  5560. .procname = "drop_unicast_in_l2_multicast",
  5561. .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
  5562. .maxlen = sizeof(int),
  5563. .mode = 0644,
  5564. .proc_handler = proc_dointvec,
  5565. },
  5566. {
  5567. .procname = "drop_unsolicited_na",
  5568. .data = &ipv6_devconf.drop_unsolicited_na,
  5569. .maxlen = sizeof(int),
  5570. .mode = 0644,
  5571. .proc_handler = proc_dointvec,
  5572. },
  5573. {
  5574. .procname = "keep_addr_on_down",
  5575. .data = &ipv6_devconf.keep_addr_on_down,
  5576. .maxlen = sizeof(int),
  5577. .mode = 0644,
  5578. .proc_handler = proc_dointvec,
  5579. },
  5580. {
  5581. .procname = "seg6_enabled",
  5582. .data = &ipv6_devconf.seg6_enabled,
  5583. .maxlen = sizeof(int),
  5584. .mode = 0644,
  5585. .proc_handler = proc_dointvec,
  5586. },
  5587. #ifdef CONFIG_IPV6_SEG6_HMAC
  5588. {
  5589. .procname = "seg6_require_hmac",
  5590. .data = &ipv6_devconf.seg6_require_hmac,
  5591. .maxlen = sizeof(int),
  5592. .mode = 0644,
  5593. .proc_handler = proc_dointvec,
  5594. },
  5595. #endif
  5596. {
  5597. .procname = "enhanced_dad",
  5598. .data = &ipv6_devconf.enhanced_dad,
  5599. .maxlen = sizeof(int),
  5600. .mode = 0644,
  5601. .proc_handler = proc_dointvec,
  5602. },
  5603. {
  5604. .procname = "addr_gen_mode",
  5605. .data = &ipv6_devconf.addr_gen_mode,
  5606. .maxlen = sizeof(int),
  5607. .mode = 0644,
  5608. .proc_handler = addrconf_sysctl_addr_gen_mode,
  5609. },
  5610. {
  5611. .procname = "disable_policy",
  5612. .data = &ipv6_devconf.disable_policy,
  5613. .maxlen = sizeof(int),
  5614. .mode = 0644,
  5615. .proc_handler = addrconf_sysctl_disable_policy,
  5616. },
  5617. {
  5618. .procname = "ndisc_tclass",
  5619. .data = &ipv6_devconf.ndisc_tclass,
  5620. .maxlen = sizeof(int),
  5621. .mode = 0644,
  5622. .proc_handler = proc_dointvec_minmax,
  5623. .extra1 = (void *)&zero,
  5624. .extra2 = (void *)&two_five_five,
  5625. },
  5626. {
  5627. /* sentinel */
  5628. }
  5629. };
  5630. static int __addrconf_sysctl_register(struct net *net, char *dev_name,
  5631. struct inet6_dev *idev, struct ipv6_devconf *p)
  5632. {
  5633. int i, ifindex;
  5634. struct ctl_table *table;
  5635. char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
  5636. table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
  5637. if (!table)
  5638. goto out;
  5639. for (i = 0; table[i].data; i++) {
  5640. table[i].data += (char *)p - (char *)&ipv6_devconf;
  5641. /* If one of these is already set, then it is not safe to
  5642. * overwrite either of them: this makes proc_dointvec_minmax
  5643. * usable.
  5644. */
  5645. if (!table[i].extra1 && !table[i].extra2) {
  5646. table[i].extra1 = idev; /* embedded; no ref */
  5647. table[i].extra2 = net;
  5648. }
  5649. }
  5650. snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
  5651. p->sysctl_header = register_net_sysctl(net, path, table);
  5652. if (!p->sysctl_header)
  5653. goto free;
  5654. if (!strcmp(dev_name, "all"))
  5655. ifindex = NETCONFA_IFINDEX_ALL;
  5656. else if (!strcmp(dev_name, "default"))
  5657. ifindex = NETCONFA_IFINDEX_DEFAULT;
  5658. else
  5659. ifindex = idev->dev->ifindex;
  5660. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
  5661. ifindex, p);
  5662. return 0;
  5663. free:
  5664. kfree(table);
  5665. out:
  5666. return -ENOBUFS;
  5667. }
  5668. static void __addrconf_sysctl_unregister(struct net *net,
  5669. struct ipv6_devconf *p, int ifindex)
  5670. {
  5671. struct ctl_table *table;
  5672. if (!p->sysctl_header)
  5673. return;
  5674. table = p->sysctl_header->ctl_table_arg;
  5675. unregister_net_sysctl_table(p->sysctl_header);
  5676. p->sysctl_header = NULL;
  5677. kfree(table);
  5678. inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
  5679. }
  5680. static int addrconf_sysctl_register(struct inet6_dev *idev)
  5681. {
  5682. int err;
  5683. if (!sysctl_dev_name_is_allowed(idev->dev->name))
  5684. return -EINVAL;
  5685. err = neigh_sysctl_register(idev->dev, idev->nd_parms,
  5686. &ndisc_ifinfo_sysctl_change);
  5687. if (err)
  5688. return err;
  5689. err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
  5690. idev, &idev->cnf);
  5691. if (err)
  5692. neigh_sysctl_unregister(idev->nd_parms);
  5693. return err;
  5694. }
  5695. static void addrconf_sysctl_unregister(struct inet6_dev *idev)
  5696. {
  5697. __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
  5698. idev->dev->ifindex);
  5699. neigh_sysctl_unregister(idev->nd_parms);
  5700. }
  5701. #endif
  5702. static int __net_init addrconf_init_net(struct net *net)
  5703. {
  5704. int err = -ENOMEM;
  5705. struct ipv6_devconf *all, *dflt;
  5706. all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
  5707. if (!all)
  5708. goto err_alloc_all;
  5709. dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
  5710. if (!dflt)
  5711. goto err_alloc_dflt;
  5712. /* these will be inherited by all namespaces */
  5713. dflt->autoconf = ipv6_defaults.autoconf;
  5714. dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
  5715. dflt->stable_secret.initialized = false;
  5716. all->stable_secret.initialized = false;
  5717. net->ipv6.devconf_all = all;
  5718. net->ipv6.devconf_dflt = dflt;
  5719. #ifdef CONFIG_SYSCTL
  5720. err = __addrconf_sysctl_register(net, "all", NULL, all);
  5721. if (err < 0)
  5722. goto err_reg_all;
  5723. err = __addrconf_sysctl_register(net, "default", NULL, dflt);
  5724. if (err < 0)
  5725. goto err_reg_dflt;
  5726. #endif
  5727. return 0;
  5728. #ifdef CONFIG_SYSCTL
  5729. err_reg_dflt:
  5730. __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
  5731. err_reg_all:
  5732. kfree(dflt);
  5733. #endif
  5734. err_alloc_dflt:
  5735. kfree(all);
  5736. err_alloc_all:
  5737. return err;
  5738. }
  5739. static void __net_exit addrconf_exit_net(struct net *net)
  5740. {
  5741. #ifdef CONFIG_SYSCTL
  5742. __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
  5743. NETCONFA_IFINDEX_DEFAULT);
  5744. __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
  5745. NETCONFA_IFINDEX_ALL);
  5746. #endif
  5747. kfree(net->ipv6.devconf_dflt);
  5748. kfree(net->ipv6.devconf_all);
  5749. }
  5750. static struct pernet_operations addrconf_ops = {
  5751. .init = addrconf_init_net,
  5752. .exit = addrconf_exit_net,
  5753. };
  5754. static struct rtnl_af_ops inet6_ops __read_mostly = {
  5755. .family = AF_INET6,
  5756. .fill_link_af = inet6_fill_link_af,
  5757. .get_link_af_size = inet6_get_link_af_size,
  5758. .validate_link_af = inet6_validate_link_af,
  5759. .set_link_af = inet6_set_link_af,
  5760. };
  5761. /*
  5762. * Init / cleanup code
  5763. */
  5764. int __init addrconf_init(void)
  5765. {
  5766. struct inet6_dev *idev;
  5767. int i, err;
  5768. err = ipv6_addr_label_init();
  5769. if (err < 0) {
  5770. pr_crit("%s: cannot initialize default policy table: %d\n",
  5771. __func__, err);
  5772. goto out;
  5773. }
  5774. err = register_pernet_subsys(&addrconf_ops);
  5775. if (err < 0)
  5776. goto out_addrlabel;
  5777. addrconf_wq = create_workqueue("ipv6_addrconf");
  5778. if (!addrconf_wq) {
  5779. err = -ENOMEM;
  5780. goto out_nowq;
  5781. }
  5782. /* The addrconf netdev notifier requires that loopback_dev
  5783. * has it's ipv6 private information allocated and setup
  5784. * before it can bring up and give link-local addresses
  5785. * to other devices which are up.
  5786. *
  5787. * Unfortunately, loopback_dev is not necessarily the first
  5788. * entry in the global dev_base list of net devices. In fact,
  5789. * it is likely to be the very last entry on that list.
  5790. * So this causes the notifier registry below to try and
  5791. * give link-local addresses to all devices besides loopback_dev
  5792. * first, then loopback_dev, which cases all the non-loopback_dev
  5793. * devices to fail to get a link-local address.
  5794. *
  5795. * So, as a temporary fix, allocate the ipv6 structure for
  5796. * loopback_dev first by hand.
  5797. * Longer term, all of the dependencies ipv6 has upon the loopback
  5798. * device and it being up should be removed.
  5799. */
  5800. rtnl_lock();
  5801. idev = ipv6_add_dev(init_net.loopback_dev);
  5802. rtnl_unlock();
  5803. if (IS_ERR(idev)) {
  5804. err = PTR_ERR(idev);
  5805. goto errlo;
  5806. }
  5807. ip6_route_init_special_entries();
  5808. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  5809. INIT_HLIST_HEAD(&inet6_addr_lst[i]);
  5810. register_netdevice_notifier(&ipv6_dev_notf);
  5811. addrconf_verify();
  5812. rtnl_af_register(&inet6_ops);
  5813. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
  5814. NULL, inet6_dump_ifinfo, 0);
  5815. if (err < 0)
  5816. goto errout;
  5817. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
  5818. inet6_rtm_newaddr, NULL, 0);
  5819. if (err < 0)
  5820. goto errout;
  5821. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
  5822. inet6_rtm_deladdr, NULL, 0);
  5823. if (err < 0)
  5824. goto errout;
  5825. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
  5826. inet6_rtm_getaddr, inet6_dump_ifaddr,
  5827. RTNL_FLAG_DOIT_UNLOCKED);
  5828. if (err < 0)
  5829. goto errout;
  5830. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
  5831. NULL, inet6_dump_ifmcaddr, 0);
  5832. if (err < 0)
  5833. goto errout;
  5834. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
  5835. NULL, inet6_dump_ifacaddr, 0);
  5836. if (err < 0)
  5837. goto errout;
  5838. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
  5839. inet6_netconf_get_devconf,
  5840. inet6_netconf_dump_devconf,
  5841. RTNL_FLAG_DOIT_UNLOCKED);
  5842. if (err < 0)
  5843. goto errout;
  5844. err = ipv6_addr_label_rtnl_register();
  5845. if (err < 0)
  5846. goto errout;
  5847. return 0;
  5848. errout:
  5849. rtnl_unregister_all(PF_INET6);
  5850. rtnl_af_unregister(&inet6_ops);
  5851. unregister_netdevice_notifier(&ipv6_dev_notf);
  5852. errlo:
  5853. destroy_workqueue(addrconf_wq);
  5854. out_nowq:
  5855. unregister_pernet_subsys(&addrconf_ops);
  5856. out_addrlabel:
  5857. ipv6_addr_label_cleanup();
  5858. out:
  5859. return err;
  5860. }
  5861. void addrconf_cleanup(void)
  5862. {
  5863. struct net_device *dev;
  5864. int i;
  5865. unregister_netdevice_notifier(&ipv6_dev_notf);
  5866. unregister_pernet_subsys(&addrconf_ops);
  5867. ipv6_addr_label_cleanup();
  5868. rtnl_af_unregister(&inet6_ops);
  5869. rtnl_lock();
  5870. /* clean dev list */
  5871. for_each_netdev(&init_net, dev) {
  5872. if (__in6_dev_get(dev) == NULL)
  5873. continue;
  5874. addrconf_ifdown(dev, 1);
  5875. }
  5876. addrconf_ifdown(init_net.loopback_dev, 2);
  5877. /*
  5878. * Check hash table.
  5879. */
  5880. spin_lock_bh(&addrconf_hash_lock);
  5881. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  5882. WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
  5883. spin_unlock_bh(&addrconf_hash_lock);
  5884. cancel_delayed_work(&addr_chk_work);
  5885. rtnl_unlock();
  5886. destroy_workqueue(addrconf_wq);
  5887. }