addrconf.c 160 KB

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