addrconf.c 165 KB

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