addrconf.c 165 KB

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