route.c 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux INET6 implementation
  4. * FIB front-end.
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. */
  9. /* Changes:
  10. *
  11. * YOSHIFUJI Hideaki @USAGI
  12. * reworked default router selection.
  13. * - respect outgoing interface
  14. * - select from (probably) reachable routers (i.e.
  15. * routers in REACHABLE, STALE, DELAY or PROBE states).
  16. * - always select the same router if it is (probably)
  17. * reachable. otherwise, round-robin the list.
  18. * Ville Nuorvala
  19. * Fixed routing subtrees.
  20. */
  21. #define pr_fmt(fmt) "IPv6: " fmt
  22. #include <linux/capability.h>
  23. #include <linux/errno.h>
  24. #include <linux/export.h>
  25. #include <linux/types.h>
  26. #include <linux/times.h>
  27. #include <linux/socket.h>
  28. #include <linux/sockios.h>
  29. #include <linux/net.h>
  30. #include <linux/route.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/in6.h>
  33. #include <linux/mroute6.h>
  34. #include <linux/init.h>
  35. #include <linux/if_arp.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/nsproxy.h>
  39. #include <linux/slab.h>
  40. #include <linux/jhash.h>
  41. #include <linux/siphash.h>
  42. #include <net/net_namespace.h>
  43. #include <net/snmp.h>
  44. #include <net/ipv6.h>
  45. #include <net/ip6_fib.h>
  46. #include <net/ip6_route.h>
  47. #include <net/ndisc.h>
  48. #include <net/addrconf.h>
  49. #include <net/tcp.h>
  50. #include <linux/rtnetlink.h>
  51. #include <net/dst.h>
  52. #include <net/dst_metadata.h>
  53. #include <net/xfrm.h>
  54. #include <net/netevent.h>
  55. #include <net/netlink.h>
  56. #include <net/rtnh.h>
  57. #include <net/lwtunnel.h>
  58. #include <net/ip_tunnels.h>
  59. #include <net/l3mdev.h>
  60. #include <net/ip.h>
  61. #include <linux/uaccess.h>
  62. #include <linux/btf_ids.h>
  63. #ifdef CONFIG_SYSCTL
  64. #include <linux/sysctl.h>
  65. #endif
  66. static int ip6_rt_type_to_error(u8 fib6_type);
  67. #define CREATE_TRACE_POINTS
  68. #include <trace/events/fib6.h>
  69. EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
  70. #undef CREATE_TRACE_POINTS
  71. enum rt6_nud_state {
  72. RT6_NUD_FAIL_HARD = -3,
  73. RT6_NUD_FAIL_PROBE = -2,
  74. RT6_NUD_FAIL_DO_RR = -1,
  75. RT6_NUD_SUCCEED = 1
  76. };
  77. INDIRECT_CALLABLE_SCOPE
  78. struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
  79. static unsigned int ip6_default_advmss(const struct dst_entry *dst);
  80. INDIRECT_CALLABLE_SCOPE
  81. unsigned int ip6_mtu(const struct dst_entry *dst);
  82. static void ip6_negative_advice(struct sock *sk,
  83. struct dst_entry *dst);
  84. static void ip6_dst_destroy(struct dst_entry *);
  85. static void ip6_dst_ifdown(struct dst_entry *,
  86. struct net_device *dev);
  87. static void ip6_dst_gc(struct dst_ops *ops);
  88. static int ip6_pkt_discard(struct sk_buff *skb);
  89. static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  90. static int ip6_pkt_prohibit(struct sk_buff *skb);
  91. static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  92. static void ip6_link_failure(struct sk_buff *skb);
  93. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  94. struct sk_buff *skb, u32 mtu,
  95. bool confirm_neigh);
  96. static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
  97. struct sk_buff *skb);
  98. static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
  99. int strict);
  100. static size_t rt6_nlmsg_size(struct fib6_info *f6i);
  101. static int rt6_fill_node(struct net *net, struct sk_buff *skb,
  102. struct fib6_info *rt, struct dst_entry *dst,
  103. struct in6_addr *dest, struct in6_addr *src,
  104. int iif, int type, u32 portid, u32 seq,
  105. unsigned int flags);
  106. static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
  107. const struct in6_addr *daddr,
  108. const struct in6_addr *saddr);
  109. #ifdef CONFIG_IPV6_ROUTE_INFO
  110. static struct fib6_info *rt6_add_route_info(struct net *net,
  111. const struct in6_addr *prefix, int prefixlen,
  112. const struct in6_addr *gwaddr,
  113. struct net_device *dev,
  114. unsigned int pref);
  115. static struct fib6_info *rt6_get_route_info(struct net *net,
  116. const struct in6_addr *prefix, int prefixlen,
  117. const struct in6_addr *gwaddr,
  118. struct net_device *dev);
  119. #endif
  120. struct uncached_list {
  121. spinlock_t lock;
  122. struct list_head head;
  123. };
  124. static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
  125. void rt6_uncached_list_add(struct rt6_info *rt)
  126. {
  127. struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
  128. rt->dst.rt_uncached_list = ul;
  129. spin_lock_bh(&ul->lock);
  130. list_add_tail(&rt->dst.rt_uncached, &ul->head);
  131. spin_unlock_bh(&ul->lock);
  132. }
  133. void rt6_uncached_list_del(struct rt6_info *rt)
  134. {
  135. if (!list_empty(&rt->dst.rt_uncached)) {
  136. struct uncached_list *ul = rt->dst.rt_uncached_list;
  137. spin_lock_bh(&ul->lock);
  138. list_del_init(&rt->dst.rt_uncached);
  139. spin_unlock_bh(&ul->lock);
  140. }
  141. }
  142. static void rt6_uncached_list_flush_dev(struct net_device *dev)
  143. {
  144. int cpu;
  145. for_each_possible_cpu(cpu) {
  146. struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
  147. struct rt6_info *rt, *safe;
  148. if (list_empty(&ul->head))
  149. continue;
  150. spin_lock_bh(&ul->lock);
  151. list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
  152. struct inet6_dev *rt_idev = rt->rt6i_idev;
  153. struct net_device *rt_dev = rt->dst.dev;
  154. bool handled = false;
  155. if (rt_idev && rt_idev->dev == dev) {
  156. rt->rt6i_idev = in6_dev_get(blackhole_netdev);
  157. in6_dev_put(rt_idev);
  158. handled = true;
  159. }
  160. if (rt_dev == dev) {
  161. rt->dst.dev = blackhole_netdev;
  162. netdev_ref_replace(rt_dev, blackhole_netdev,
  163. &rt->dst.dev_tracker,
  164. GFP_ATOMIC);
  165. handled = true;
  166. }
  167. if (handled)
  168. list_del_init(&rt->dst.rt_uncached);
  169. }
  170. spin_unlock_bh(&ul->lock);
  171. }
  172. }
  173. static inline const void *choose_neigh_daddr(const struct in6_addr *p,
  174. struct sk_buff *skb,
  175. const void *daddr)
  176. {
  177. if (!ipv6_addr_any(p))
  178. return (const void *) p;
  179. else if (skb)
  180. return &ipv6_hdr(skb)->daddr;
  181. return daddr;
  182. }
  183. struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
  184. struct net_device *dev,
  185. struct sk_buff *skb,
  186. const void *daddr)
  187. {
  188. struct neighbour *n;
  189. daddr = choose_neigh_daddr(gw, skb, daddr);
  190. n = __ipv6_neigh_lookup(dev, daddr);
  191. if (n)
  192. return n;
  193. n = neigh_create(&nd_tbl, daddr, dev);
  194. return IS_ERR(n) ? NULL : n;
  195. }
  196. static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
  197. struct sk_buff *skb,
  198. const void *daddr)
  199. {
  200. const struct rt6_info *rt = dst_rt6_info(dst);
  201. return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
  202. dst->dev, skb, daddr);
  203. }
  204. static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
  205. {
  206. const struct rt6_info *rt = dst_rt6_info(dst);
  207. struct net_device *dev = dst->dev;
  208. daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
  209. if (!daddr)
  210. return;
  211. if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
  212. return;
  213. if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
  214. return;
  215. __ipv6_confirm_neigh(dev, daddr);
  216. }
  217. static struct dst_ops ip6_dst_ops_template = {
  218. .family = AF_INET6,
  219. .gc = ip6_dst_gc,
  220. .gc_thresh = 1024,
  221. .check = ip6_dst_check,
  222. .default_advmss = ip6_default_advmss,
  223. .mtu = ip6_mtu,
  224. .cow_metrics = dst_cow_metrics_generic,
  225. .destroy = ip6_dst_destroy,
  226. .ifdown = ip6_dst_ifdown,
  227. .negative_advice = ip6_negative_advice,
  228. .link_failure = ip6_link_failure,
  229. .update_pmtu = ip6_rt_update_pmtu,
  230. .redirect = rt6_do_redirect,
  231. .local_out = __ip6_local_out,
  232. .neigh_lookup = ip6_dst_neigh_lookup,
  233. .confirm_neigh = ip6_confirm_neigh,
  234. };
  235. static struct dst_ops ip6_dst_blackhole_ops = {
  236. .family = AF_INET6,
  237. .default_advmss = ip6_default_advmss,
  238. .neigh_lookup = ip6_dst_neigh_lookup,
  239. .check = ip6_dst_check,
  240. .destroy = ip6_dst_destroy,
  241. .cow_metrics = dst_cow_metrics_generic,
  242. .update_pmtu = dst_blackhole_update_pmtu,
  243. .redirect = dst_blackhole_redirect,
  244. .mtu = dst_blackhole_mtu,
  245. };
  246. static const u32 ip6_template_metrics[RTAX_MAX] = {
  247. [RTAX_HOPLIMIT - 1] = 0,
  248. };
  249. static const struct fib6_info fib6_null_entry_template = {
  250. .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
  251. .fib6_protocol = RTPROT_KERNEL,
  252. .fib6_metric = ~(u32)0,
  253. .fib6_ref = REFCOUNT_INIT(1),
  254. .fib6_type = RTN_UNREACHABLE,
  255. .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
  256. };
  257. static const struct rt6_info ip6_null_entry_template = {
  258. .dst = {
  259. .__rcuref = RCUREF_INIT(1),
  260. .__use = 1,
  261. .obsolete = DST_OBSOLETE_FORCE_CHK,
  262. .error = -ENETUNREACH,
  263. .input = ip6_pkt_discard,
  264. .output = ip6_pkt_discard_out,
  265. },
  266. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  267. };
  268. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  269. static const struct rt6_info ip6_prohibit_entry_template = {
  270. .dst = {
  271. .__rcuref = RCUREF_INIT(1),
  272. .__use = 1,
  273. .obsolete = DST_OBSOLETE_FORCE_CHK,
  274. .error = -EACCES,
  275. .input = ip6_pkt_prohibit,
  276. .output = ip6_pkt_prohibit_out,
  277. },
  278. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  279. };
  280. static const struct rt6_info ip6_blk_hole_entry_template = {
  281. .dst = {
  282. .__rcuref = RCUREF_INIT(1),
  283. .__use = 1,
  284. .obsolete = DST_OBSOLETE_FORCE_CHK,
  285. .error = -EINVAL,
  286. .input = dst_discard,
  287. .output = dst_discard_out,
  288. },
  289. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  290. };
  291. #endif
  292. static void rt6_info_init(struct rt6_info *rt)
  293. {
  294. memset_after(rt, 0, dst);
  295. }
  296. /* allocate dst with ip6_dst_ops */
  297. struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
  298. int flags)
  299. {
  300. struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
  301. DST_OBSOLETE_FORCE_CHK, flags);
  302. if (rt) {
  303. rt6_info_init(rt);
  304. atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
  305. }
  306. return rt;
  307. }
  308. EXPORT_SYMBOL(ip6_dst_alloc);
  309. static void ip6_dst_destroy(struct dst_entry *dst)
  310. {
  311. struct rt6_info *rt = dst_rt6_info(dst);
  312. struct fib6_info *from;
  313. struct inet6_dev *idev;
  314. ip_dst_metrics_put(dst);
  315. rt6_uncached_list_del(rt);
  316. idev = rt->rt6i_idev;
  317. if (idev) {
  318. rt->rt6i_idev = NULL;
  319. in6_dev_put(idev);
  320. }
  321. from = unrcu_pointer(xchg(&rt->from, NULL));
  322. fib6_info_release(from);
  323. }
  324. static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  325. {
  326. struct rt6_info *rt = dst_rt6_info(dst);
  327. struct inet6_dev *idev = rt->rt6i_idev;
  328. struct fib6_info *from;
  329. if (idev && idev->dev != blackhole_netdev) {
  330. struct inet6_dev *blackhole_idev = in6_dev_get(blackhole_netdev);
  331. if (blackhole_idev) {
  332. rt->rt6i_idev = blackhole_idev;
  333. in6_dev_put(idev);
  334. }
  335. }
  336. from = unrcu_pointer(xchg(&rt->from, NULL));
  337. fib6_info_release(from);
  338. }
  339. static bool __rt6_check_expired(const struct rt6_info *rt)
  340. {
  341. if (rt->rt6i_flags & RTF_EXPIRES)
  342. return time_after(jiffies, rt->dst.expires);
  343. else
  344. return false;
  345. }
  346. static bool rt6_check_expired(const struct rt6_info *rt)
  347. {
  348. struct fib6_info *from;
  349. from = rcu_dereference(rt->from);
  350. if (rt->rt6i_flags & RTF_EXPIRES) {
  351. if (time_after(jiffies, rt->dst.expires))
  352. return true;
  353. } else if (from) {
  354. return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
  355. fib6_check_expired(from);
  356. }
  357. return false;
  358. }
  359. static struct fib6_info *
  360. rt6_multipath_first_sibling_rcu(const struct fib6_info *rt)
  361. {
  362. struct fib6_info *iter;
  363. struct fib6_node *fn;
  364. fn = rcu_dereference(rt->fib6_node);
  365. if (!fn)
  366. goto out;
  367. iter = rcu_dereference(fn->leaf);
  368. if (!iter)
  369. goto out;
  370. while (iter) {
  371. if (iter->fib6_metric == rt->fib6_metric &&
  372. rt6_qualify_for_ecmp(iter))
  373. return iter;
  374. iter = rcu_dereference(iter->fib6_next);
  375. }
  376. out:
  377. return NULL;
  378. }
  379. void fib6_select_path(const struct net *net, struct fib6_result *res,
  380. struct flowi6 *fl6, int oif, bool have_oif_match,
  381. const struct sk_buff *skb, int strict)
  382. {
  383. struct fib6_info *first, *match = res->f6i;
  384. struct fib6_info *sibling;
  385. int hash;
  386. if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
  387. goto out;
  388. if (match->nh && have_oif_match && res->nh)
  389. return;
  390. if (skb)
  391. IP6CB(skb)->flags |= IP6SKB_MULTIPATH;
  392. /* We might have already computed the hash for ICMPv6 errors. In such
  393. * case it will always be non-zero. Otherwise now is the time to do it.
  394. */
  395. if (!fl6->mp_hash &&
  396. (!match->nh || nexthop_is_multipath(match->nh)))
  397. fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
  398. if (unlikely(match->nh)) {
  399. nexthop_path_fib6_result(res, fl6->mp_hash);
  400. return;
  401. }
  402. first = rt6_multipath_first_sibling_rcu(match);
  403. if (!first)
  404. goto out;
  405. hash = fl6->mp_hash;
  406. if (hash <= atomic_read(&first->fib6_nh->fib_nh_upper_bound)) {
  407. if (rt6_score_route(first->fib6_nh, first->fib6_flags, oif,
  408. strict) >= 0)
  409. match = first;
  410. goto out;
  411. }
  412. list_for_each_entry_rcu(sibling, &first->fib6_siblings,
  413. fib6_siblings) {
  414. const struct fib6_nh *nh = sibling->fib6_nh;
  415. int nh_upper_bound;
  416. nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
  417. if (hash > nh_upper_bound)
  418. continue;
  419. if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
  420. break;
  421. match = sibling;
  422. break;
  423. }
  424. out:
  425. res->f6i = match;
  426. res->nh = match->fib6_nh;
  427. }
  428. /*
  429. * Route lookup. rcu_read_lock() should be held.
  430. */
  431. static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
  432. const struct in6_addr *saddr, int oif, int flags)
  433. {
  434. const struct net_device *dev;
  435. if (nh->fib_nh_flags & RTNH_F_DEAD)
  436. return false;
  437. dev = nh->fib_nh_dev;
  438. if (oif) {
  439. if (dev->ifindex == oif)
  440. return true;
  441. } else {
  442. if (ipv6_chk_addr(net, saddr, dev,
  443. flags & RT6_LOOKUP_F_IFACE))
  444. return true;
  445. }
  446. return false;
  447. }
  448. struct fib6_nh_dm_arg {
  449. struct net *net;
  450. const struct in6_addr *saddr;
  451. int oif;
  452. int flags;
  453. struct fib6_nh *nh;
  454. };
  455. static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
  456. {
  457. struct fib6_nh_dm_arg *arg = _arg;
  458. arg->nh = nh;
  459. return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
  460. arg->flags);
  461. }
  462. /* returns fib6_nh from nexthop or NULL */
  463. static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
  464. struct fib6_result *res,
  465. const struct in6_addr *saddr,
  466. int oif, int flags)
  467. {
  468. struct fib6_nh_dm_arg arg = {
  469. .net = net,
  470. .saddr = saddr,
  471. .oif = oif,
  472. .flags = flags,
  473. };
  474. if (nexthop_is_blackhole(nh))
  475. return NULL;
  476. if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
  477. return arg.nh;
  478. return NULL;
  479. }
  480. static void rt6_device_match(struct net *net, struct fib6_result *res,
  481. const struct in6_addr *saddr, int oif, int flags)
  482. {
  483. struct fib6_info *f6i = res->f6i;
  484. struct fib6_info *spf6i;
  485. struct fib6_nh *nh;
  486. if (!oif && ipv6_addr_any(saddr)) {
  487. if (unlikely(f6i->nh)) {
  488. nh = nexthop_fib6_nh(f6i->nh);
  489. if (nexthop_is_blackhole(f6i->nh))
  490. goto out_blackhole;
  491. } else {
  492. nh = f6i->fib6_nh;
  493. }
  494. if (!(nh->fib_nh_flags & RTNH_F_DEAD))
  495. goto out;
  496. }
  497. for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
  498. bool matched = false;
  499. if (unlikely(spf6i->nh)) {
  500. nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
  501. oif, flags);
  502. if (nh)
  503. matched = true;
  504. } else {
  505. nh = spf6i->fib6_nh;
  506. if (__rt6_device_match(net, nh, saddr, oif, flags))
  507. matched = true;
  508. }
  509. if (matched) {
  510. res->f6i = spf6i;
  511. goto out;
  512. }
  513. }
  514. if (oif && flags & RT6_LOOKUP_F_IFACE) {
  515. res->f6i = net->ipv6.fib6_null_entry;
  516. nh = res->f6i->fib6_nh;
  517. goto out;
  518. }
  519. if (unlikely(f6i->nh)) {
  520. nh = nexthop_fib6_nh(f6i->nh);
  521. if (nexthop_is_blackhole(f6i->nh))
  522. goto out_blackhole;
  523. } else {
  524. nh = f6i->fib6_nh;
  525. }
  526. if (nh->fib_nh_flags & RTNH_F_DEAD) {
  527. res->f6i = net->ipv6.fib6_null_entry;
  528. nh = res->f6i->fib6_nh;
  529. }
  530. out:
  531. res->nh = nh;
  532. res->fib6_type = res->f6i->fib6_type;
  533. res->fib6_flags = res->f6i->fib6_flags;
  534. return;
  535. out_blackhole:
  536. res->fib6_flags |= RTF_REJECT;
  537. res->fib6_type = RTN_BLACKHOLE;
  538. res->nh = nh;
  539. }
  540. #ifdef CONFIG_IPV6_ROUTER_PREF
  541. struct __rt6_probe_work {
  542. struct work_struct work;
  543. struct in6_addr target;
  544. struct net_device *dev;
  545. netdevice_tracker dev_tracker;
  546. };
  547. static void rt6_probe_deferred(struct work_struct *w)
  548. {
  549. struct in6_addr mcaddr;
  550. struct __rt6_probe_work *work =
  551. container_of(w, struct __rt6_probe_work, work);
  552. addrconf_addr_solict_mult(&work->target, &mcaddr);
  553. ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
  554. netdev_put(work->dev, &work->dev_tracker);
  555. kfree(work);
  556. }
  557. static void rt6_probe(struct fib6_nh *fib6_nh)
  558. {
  559. struct __rt6_probe_work *work = NULL;
  560. const struct in6_addr *nh_gw;
  561. unsigned long last_probe;
  562. struct neighbour *neigh;
  563. struct net_device *dev;
  564. struct inet6_dev *idev;
  565. /*
  566. * Okay, this does not seem to be appropriate
  567. * for now, however, we need to check if it
  568. * is really so; aka Router Reachability Probing.
  569. *
  570. * Router Reachability Probe MUST be rate-limited
  571. * to no more than one per minute.
  572. */
  573. if (!fib6_nh->fib_nh_gw_family)
  574. return;
  575. nh_gw = &fib6_nh->fib_nh_gw6;
  576. dev = fib6_nh->fib_nh_dev;
  577. rcu_read_lock();
  578. last_probe = READ_ONCE(fib6_nh->last_probe);
  579. idev = __in6_dev_get(dev);
  580. if (!idev)
  581. goto out;
  582. neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
  583. if (neigh) {
  584. if (READ_ONCE(neigh->nud_state) & NUD_VALID)
  585. goto out;
  586. write_lock_bh(&neigh->lock);
  587. if (!(neigh->nud_state & NUD_VALID) &&
  588. time_after(jiffies,
  589. neigh->updated +
  590. READ_ONCE(idev->cnf.rtr_probe_interval))) {
  591. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  592. if (work)
  593. __neigh_set_probe_once(neigh);
  594. }
  595. write_unlock_bh(&neigh->lock);
  596. } else if (time_after(jiffies, last_probe +
  597. READ_ONCE(idev->cnf.rtr_probe_interval))) {
  598. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  599. }
  600. if (!work || cmpxchg(&fib6_nh->last_probe,
  601. last_probe, jiffies) != last_probe) {
  602. kfree(work);
  603. } else {
  604. INIT_WORK(&work->work, rt6_probe_deferred);
  605. work->target = *nh_gw;
  606. netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC);
  607. work->dev = dev;
  608. schedule_work(&work->work);
  609. }
  610. out:
  611. rcu_read_unlock();
  612. }
  613. #else
  614. static inline void rt6_probe(struct fib6_nh *fib6_nh)
  615. {
  616. }
  617. #endif
  618. /*
  619. * Default Router Selection (RFC 2461 6.3.6)
  620. */
  621. static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
  622. {
  623. enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
  624. struct neighbour *neigh;
  625. rcu_read_lock();
  626. neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
  627. &fib6_nh->fib_nh_gw6);
  628. if (neigh) {
  629. u8 nud_state = READ_ONCE(neigh->nud_state);
  630. if (nud_state & NUD_VALID)
  631. ret = RT6_NUD_SUCCEED;
  632. #ifdef CONFIG_IPV6_ROUTER_PREF
  633. else if (!(nud_state & NUD_FAILED))
  634. ret = RT6_NUD_SUCCEED;
  635. else
  636. ret = RT6_NUD_FAIL_PROBE;
  637. #endif
  638. } else {
  639. ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
  640. RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
  641. }
  642. rcu_read_unlock();
  643. return ret;
  644. }
  645. static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
  646. int strict)
  647. {
  648. int m = 0;
  649. if (!oif || nh->fib_nh_dev->ifindex == oif)
  650. m = 2;
  651. if (!m && (strict & RT6_LOOKUP_F_IFACE))
  652. return RT6_NUD_FAIL_HARD;
  653. #ifdef CONFIG_IPV6_ROUTER_PREF
  654. m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
  655. #endif
  656. if ((strict & RT6_LOOKUP_F_REACHABLE) &&
  657. !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
  658. int n = rt6_check_neigh(nh);
  659. if (n < 0)
  660. return n;
  661. }
  662. return m;
  663. }
  664. static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
  665. int oif, int strict, int *mpri, bool *do_rr)
  666. {
  667. bool match_do_rr = false;
  668. bool rc = false;
  669. int m;
  670. if (nh->fib_nh_flags & RTNH_F_DEAD)
  671. goto out;
  672. if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
  673. nh->fib_nh_flags & RTNH_F_LINKDOWN &&
  674. !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
  675. goto out;
  676. m = rt6_score_route(nh, fib6_flags, oif, strict);
  677. if (m == RT6_NUD_FAIL_DO_RR) {
  678. match_do_rr = true;
  679. m = 0; /* lowest valid score */
  680. } else if (m == RT6_NUD_FAIL_HARD) {
  681. goto out;
  682. }
  683. if (strict & RT6_LOOKUP_F_REACHABLE)
  684. rt6_probe(nh);
  685. /* note that m can be RT6_NUD_FAIL_PROBE at this point */
  686. if (m > *mpri) {
  687. *do_rr = match_do_rr;
  688. *mpri = m;
  689. rc = true;
  690. }
  691. out:
  692. return rc;
  693. }
  694. struct fib6_nh_frl_arg {
  695. u32 flags;
  696. int oif;
  697. int strict;
  698. int *mpri;
  699. bool *do_rr;
  700. struct fib6_nh *nh;
  701. };
  702. static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
  703. {
  704. struct fib6_nh_frl_arg *arg = _arg;
  705. arg->nh = nh;
  706. return find_match(nh, arg->flags, arg->oif, arg->strict,
  707. arg->mpri, arg->do_rr);
  708. }
  709. static void __find_rr_leaf(struct fib6_info *f6i_start,
  710. struct fib6_info *nomatch, u32 metric,
  711. struct fib6_result *res, struct fib6_info **cont,
  712. int oif, int strict, bool *do_rr, int *mpri)
  713. {
  714. struct fib6_info *f6i;
  715. for (f6i = f6i_start;
  716. f6i && f6i != nomatch;
  717. f6i = rcu_dereference(f6i->fib6_next)) {
  718. bool matched = false;
  719. struct fib6_nh *nh;
  720. if (cont && f6i->fib6_metric != metric) {
  721. *cont = f6i;
  722. return;
  723. }
  724. if (fib6_check_expired(f6i))
  725. continue;
  726. if (unlikely(f6i->nh)) {
  727. struct fib6_nh_frl_arg arg = {
  728. .flags = f6i->fib6_flags,
  729. .oif = oif,
  730. .strict = strict,
  731. .mpri = mpri,
  732. .do_rr = do_rr
  733. };
  734. if (nexthop_is_blackhole(f6i->nh)) {
  735. res->fib6_flags = RTF_REJECT;
  736. res->fib6_type = RTN_BLACKHOLE;
  737. res->f6i = f6i;
  738. res->nh = nexthop_fib6_nh(f6i->nh);
  739. return;
  740. }
  741. if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
  742. &arg)) {
  743. matched = true;
  744. nh = arg.nh;
  745. }
  746. } else {
  747. nh = f6i->fib6_nh;
  748. if (find_match(nh, f6i->fib6_flags, oif, strict,
  749. mpri, do_rr))
  750. matched = true;
  751. }
  752. if (matched) {
  753. res->f6i = f6i;
  754. res->nh = nh;
  755. res->fib6_flags = f6i->fib6_flags;
  756. res->fib6_type = f6i->fib6_type;
  757. }
  758. }
  759. }
  760. static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
  761. struct fib6_info *rr_head, int oif, int strict,
  762. bool *do_rr, struct fib6_result *res)
  763. {
  764. u32 metric = rr_head->fib6_metric;
  765. struct fib6_info *cont = NULL;
  766. int mpri = -1;
  767. __find_rr_leaf(rr_head, NULL, metric, res, &cont,
  768. oif, strict, do_rr, &mpri);
  769. __find_rr_leaf(leaf, rr_head, metric, res, &cont,
  770. oif, strict, do_rr, &mpri);
  771. if (res->f6i || !cont)
  772. return;
  773. __find_rr_leaf(cont, NULL, metric, res, NULL,
  774. oif, strict, do_rr, &mpri);
  775. }
  776. static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
  777. struct fib6_result *res, int strict)
  778. {
  779. struct fib6_info *leaf = rcu_dereference(fn->leaf);
  780. struct fib6_info *rt0;
  781. bool do_rr = false;
  782. int key_plen;
  783. /* make sure this function or its helpers sets f6i */
  784. res->f6i = NULL;
  785. if (!leaf || leaf == net->ipv6.fib6_null_entry)
  786. goto out;
  787. rt0 = rcu_dereference(fn->rr_ptr);
  788. if (!rt0)
  789. rt0 = leaf;
  790. /* Double check to make sure fn is not an intermediate node
  791. * and fn->leaf does not points to its child's leaf
  792. * (This might happen if all routes under fn are deleted from
  793. * the tree and fib6_repair_tree() is called on the node.)
  794. */
  795. key_plen = rt0->fib6_dst.plen;
  796. #ifdef CONFIG_IPV6_SUBTREES
  797. if (rt0->fib6_src.plen)
  798. key_plen = rt0->fib6_src.plen;
  799. #endif
  800. if (fn->fn_bit != key_plen)
  801. goto out;
  802. find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
  803. if (do_rr) {
  804. struct fib6_info *next = rcu_dereference(rt0->fib6_next);
  805. /* no entries matched; do round-robin */
  806. if (!next || next->fib6_metric != rt0->fib6_metric)
  807. next = leaf;
  808. if (next != rt0) {
  809. spin_lock_bh(&leaf->fib6_table->tb6_lock);
  810. /* make sure next is not being deleted from the tree */
  811. if (next->fib6_node)
  812. rcu_assign_pointer(fn->rr_ptr, next);
  813. spin_unlock_bh(&leaf->fib6_table->tb6_lock);
  814. }
  815. }
  816. out:
  817. if (!res->f6i) {
  818. res->f6i = net->ipv6.fib6_null_entry;
  819. res->nh = res->f6i->fib6_nh;
  820. res->fib6_flags = res->f6i->fib6_flags;
  821. res->fib6_type = res->f6i->fib6_type;
  822. }
  823. }
  824. static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
  825. {
  826. return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
  827. res->nh->fib_nh_gw_family;
  828. }
  829. #ifdef CONFIG_IPV6_ROUTE_INFO
  830. int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
  831. const struct in6_addr *gwaddr)
  832. {
  833. struct net *net = dev_net(dev);
  834. struct route_info *rinfo = (struct route_info *) opt;
  835. struct in6_addr prefix_buf, *prefix;
  836. struct fib6_table *table;
  837. unsigned int pref;
  838. unsigned long lifetime;
  839. struct fib6_info *rt;
  840. if (len < sizeof(struct route_info)) {
  841. return -EINVAL;
  842. }
  843. /* Sanity check for prefix_len and length */
  844. if (rinfo->length > 3) {
  845. return -EINVAL;
  846. } else if (rinfo->prefix_len > 128) {
  847. return -EINVAL;
  848. } else if (rinfo->prefix_len > 64) {
  849. if (rinfo->length < 2) {
  850. return -EINVAL;
  851. }
  852. } else if (rinfo->prefix_len > 0) {
  853. if (rinfo->length < 1) {
  854. return -EINVAL;
  855. }
  856. }
  857. pref = rinfo->route_pref;
  858. if (pref == ICMPV6_ROUTER_PREF_INVALID)
  859. return -EINVAL;
  860. lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
  861. if (rinfo->length == 3)
  862. prefix = (struct in6_addr *)rinfo->prefix;
  863. else {
  864. /* this function is safe */
  865. ipv6_addr_prefix(&prefix_buf,
  866. (struct in6_addr *)rinfo->prefix,
  867. rinfo->prefix_len);
  868. prefix = &prefix_buf;
  869. }
  870. if (rinfo->prefix_len == 0)
  871. rt = rt6_get_dflt_router(net, gwaddr, dev);
  872. else
  873. rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
  874. gwaddr, dev);
  875. if (rt && !lifetime) {
  876. ip6_del_rt(net, rt, false);
  877. rt = NULL;
  878. }
  879. if (!rt && lifetime)
  880. rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
  881. dev, pref);
  882. else if (rt)
  883. rt->fib6_flags = RTF_ROUTEINFO |
  884. (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  885. if (rt) {
  886. table = rt->fib6_table;
  887. spin_lock_bh(&table->tb6_lock);
  888. if (!addrconf_finite_timeout(lifetime)) {
  889. fib6_clean_expires(rt);
  890. fib6_remove_gc_list(rt);
  891. } else {
  892. fib6_set_expires(rt, jiffies + HZ * lifetime);
  893. fib6_add_gc_list(rt);
  894. }
  895. spin_unlock_bh(&table->tb6_lock);
  896. fib6_info_release(rt);
  897. }
  898. return 0;
  899. }
  900. #endif
  901. /*
  902. * Misc support functions
  903. */
  904. /* called with rcu_lock held */
  905. static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
  906. {
  907. struct net_device *dev = res->nh->fib_nh_dev;
  908. if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
  909. /* for copies of local routes, dst->dev needs to be the
  910. * device if it is a master device, the master device if
  911. * device is enslaved, and the loopback as the default
  912. */
  913. if (netif_is_l3_slave(dev) &&
  914. !rt6_need_strict(&res->f6i->fib6_dst.addr))
  915. dev = l3mdev_master_dev_rcu(dev);
  916. else if (!netif_is_l3_master(dev))
  917. dev = dev_net(dev)->loopback_dev;
  918. /* last case is netif_is_l3_master(dev) is true in which
  919. * case we want dev returned to be dev
  920. */
  921. }
  922. return dev;
  923. }
  924. static const int fib6_prop[RTN_MAX + 1] = {
  925. [RTN_UNSPEC] = 0,
  926. [RTN_UNICAST] = 0,
  927. [RTN_LOCAL] = 0,
  928. [RTN_BROADCAST] = 0,
  929. [RTN_ANYCAST] = 0,
  930. [RTN_MULTICAST] = 0,
  931. [RTN_BLACKHOLE] = -EINVAL,
  932. [RTN_UNREACHABLE] = -EHOSTUNREACH,
  933. [RTN_PROHIBIT] = -EACCES,
  934. [RTN_THROW] = -EAGAIN,
  935. [RTN_NAT] = -EINVAL,
  936. [RTN_XRESOLVE] = -EINVAL,
  937. };
  938. static int ip6_rt_type_to_error(u8 fib6_type)
  939. {
  940. return fib6_prop[fib6_type];
  941. }
  942. static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
  943. {
  944. unsigned short flags = 0;
  945. if (rt->dst_nocount)
  946. flags |= DST_NOCOUNT;
  947. if (rt->dst_nopolicy)
  948. flags |= DST_NOPOLICY;
  949. return flags;
  950. }
  951. static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
  952. {
  953. rt->dst.error = ip6_rt_type_to_error(fib6_type);
  954. switch (fib6_type) {
  955. case RTN_BLACKHOLE:
  956. rt->dst.output = dst_discard_out;
  957. rt->dst.input = dst_discard;
  958. break;
  959. case RTN_PROHIBIT:
  960. rt->dst.output = ip6_pkt_prohibit_out;
  961. rt->dst.input = ip6_pkt_prohibit;
  962. break;
  963. case RTN_THROW:
  964. case RTN_UNREACHABLE:
  965. default:
  966. rt->dst.output = ip6_pkt_discard_out;
  967. rt->dst.input = ip6_pkt_discard;
  968. break;
  969. }
  970. }
  971. static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
  972. {
  973. struct fib6_info *f6i = res->f6i;
  974. if (res->fib6_flags & RTF_REJECT) {
  975. ip6_rt_init_dst_reject(rt, res->fib6_type);
  976. return;
  977. }
  978. rt->dst.error = 0;
  979. rt->dst.output = ip6_output;
  980. if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
  981. rt->dst.input = ip6_input;
  982. } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
  983. rt->dst.input = ip6_mc_input;
  984. } else {
  985. rt->dst.input = ip6_forward;
  986. }
  987. if (res->nh->fib_nh_lws) {
  988. rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
  989. lwtunnel_set_redirect(&rt->dst);
  990. }
  991. rt->dst.lastuse = jiffies;
  992. }
  993. /* Caller must already hold reference to @from */
  994. static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
  995. {
  996. rt->rt6i_flags &= ~RTF_EXPIRES;
  997. rcu_assign_pointer(rt->from, from);
  998. ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
  999. }
  1000. /* Caller must already hold reference to f6i in result */
  1001. static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
  1002. {
  1003. const struct fib6_nh *nh = res->nh;
  1004. const struct net_device *dev = nh->fib_nh_dev;
  1005. struct fib6_info *f6i = res->f6i;
  1006. ip6_rt_init_dst(rt, res);
  1007. rt->rt6i_dst = f6i->fib6_dst;
  1008. rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
  1009. rt->rt6i_flags = res->fib6_flags;
  1010. if (nh->fib_nh_gw_family) {
  1011. rt->rt6i_gateway = nh->fib_nh_gw6;
  1012. rt->rt6i_flags |= RTF_GATEWAY;
  1013. }
  1014. rt6_set_from(rt, f6i);
  1015. #ifdef CONFIG_IPV6_SUBTREES
  1016. rt->rt6i_src = f6i->fib6_src;
  1017. #endif
  1018. }
  1019. static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
  1020. struct in6_addr *saddr)
  1021. {
  1022. struct fib6_node *pn, *sn;
  1023. while (1) {
  1024. if (fn->fn_flags & RTN_TL_ROOT)
  1025. return NULL;
  1026. pn = rcu_dereference(fn->parent);
  1027. sn = FIB6_SUBTREE(pn);
  1028. if (sn && sn != fn)
  1029. fn = fib6_node_lookup(sn, NULL, saddr);
  1030. else
  1031. fn = pn;
  1032. if (fn->fn_flags & RTN_RTINFO)
  1033. return fn;
  1034. }
  1035. }
  1036. static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
  1037. {
  1038. struct rt6_info *rt = *prt;
  1039. if (dst_hold_safe(&rt->dst))
  1040. return true;
  1041. if (net) {
  1042. rt = net->ipv6.ip6_null_entry;
  1043. dst_hold(&rt->dst);
  1044. } else {
  1045. rt = NULL;
  1046. }
  1047. *prt = rt;
  1048. return false;
  1049. }
  1050. /* called with rcu_lock held */
  1051. static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
  1052. {
  1053. struct net_device *dev = res->nh->fib_nh_dev;
  1054. struct fib6_info *f6i = res->f6i;
  1055. unsigned short flags;
  1056. struct rt6_info *nrt;
  1057. if (!fib6_info_hold_safe(f6i))
  1058. goto fallback;
  1059. flags = fib6_info_dst_flags(f6i);
  1060. nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
  1061. if (!nrt) {
  1062. fib6_info_release(f6i);
  1063. goto fallback;
  1064. }
  1065. ip6_rt_copy_init(nrt, res);
  1066. return nrt;
  1067. fallback:
  1068. nrt = dev_net(dev)->ipv6.ip6_null_entry;
  1069. dst_hold(&nrt->dst);
  1070. return nrt;
  1071. }
  1072. INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
  1073. struct fib6_table *table,
  1074. struct flowi6 *fl6,
  1075. const struct sk_buff *skb,
  1076. int flags)
  1077. {
  1078. struct fib6_result res = {};
  1079. struct fib6_node *fn;
  1080. struct rt6_info *rt;
  1081. rcu_read_lock();
  1082. fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  1083. restart:
  1084. res.f6i = rcu_dereference(fn->leaf);
  1085. if (!res.f6i)
  1086. res.f6i = net->ipv6.fib6_null_entry;
  1087. else
  1088. rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
  1089. flags);
  1090. if (res.f6i == net->ipv6.fib6_null_entry) {
  1091. fn = fib6_backtrack(fn, &fl6->saddr);
  1092. if (fn)
  1093. goto restart;
  1094. rt = net->ipv6.ip6_null_entry;
  1095. dst_hold(&rt->dst);
  1096. goto out;
  1097. } else if (res.fib6_flags & RTF_REJECT) {
  1098. goto do_create;
  1099. }
  1100. fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
  1101. fl6->flowi6_oif != 0, skb, flags);
  1102. /* Search through exception table */
  1103. rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
  1104. if (rt) {
  1105. if (ip6_hold_safe(net, &rt))
  1106. dst_use_noref(&rt->dst, jiffies);
  1107. } else {
  1108. do_create:
  1109. rt = ip6_create_rt_rcu(&res);
  1110. }
  1111. out:
  1112. trace_fib6_table_lookup(net, &res, table, fl6);
  1113. rcu_read_unlock();
  1114. return rt;
  1115. }
  1116. struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
  1117. const struct sk_buff *skb, int flags)
  1118. {
  1119. return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
  1120. }
  1121. EXPORT_SYMBOL_GPL(ip6_route_lookup);
  1122. struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
  1123. const struct in6_addr *saddr, int oif,
  1124. const struct sk_buff *skb, int strict)
  1125. {
  1126. struct flowi6 fl6 = {
  1127. .flowi6_oif = oif,
  1128. .daddr = *daddr,
  1129. };
  1130. struct dst_entry *dst;
  1131. int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
  1132. if (saddr) {
  1133. memcpy(&fl6.saddr, saddr, sizeof(*saddr));
  1134. flags |= RT6_LOOKUP_F_HAS_SADDR;
  1135. }
  1136. dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
  1137. if (dst->error == 0)
  1138. return dst_rt6_info(dst);
  1139. dst_release(dst);
  1140. return NULL;
  1141. }
  1142. EXPORT_SYMBOL(rt6_lookup);
  1143. /* ip6_ins_rt is called with FREE table->tb6_lock.
  1144. * It takes new route entry, the addition fails by any reason the
  1145. * route is released.
  1146. * Caller must hold dst before calling it.
  1147. */
  1148. static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
  1149. struct netlink_ext_ack *extack)
  1150. {
  1151. int err;
  1152. struct fib6_table *table;
  1153. table = rt->fib6_table;
  1154. spin_lock_bh(&table->tb6_lock);
  1155. err = fib6_add(&table->tb6_root, rt, info, extack);
  1156. spin_unlock_bh(&table->tb6_lock);
  1157. return err;
  1158. }
  1159. int ip6_ins_rt(struct net *net, struct fib6_info *rt)
  1160. {
  1161. struct nl_info info = { .nl_net = net, };
  1162. return __ip6_ins_rt(rt, &info, NULL);
  1163. }
  1164. static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
  1165. const struct in6_addr *daddr,
  1166. const struct in6_addr *saddr)
  1167. {
  1168. struct fib6_info *f6i = res->f6i;
  1169. struct net_device *dev;
  1170. struct rt6_info *rt;
  1171. /*
  1172. * Clone the route.
  1173. */
  1174. if (!fib6_info_hold_safe(f6i))
  1175. return NULL;
  1176. dev = ip6_rt_get_dev_rcu(res);
  1177. rt = ip6_dst_alloc(dev_net(dev), dev, 0);
  1178. if (!rt) {
  1179. fib6_info_release(f6i);
  1180. return NULL;
  1181. }
  1182. ip6_rt_copy_init(rt, res);
  1183. rt->rt6i_flags |= RTF_CACHE;
  1184. rt->rt6i_dst.addr = *daddr;
  1185. rt->rt6i_dst.plen = 128;
  1186. if (!rt6_is_gw_or_nonexthop(res)) {
  1187. if (f6i->fib6_dst.plen != 128 &&
  1188. ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
  1189. rt->rt6i_flags |= RTF_ANYCAST;
  1190. #ifdef CONFIG_IPV6_SUBTREES
  1191. if (rt->rt6i_src.plen && saddr) {
  1192. rt->rt6i_src.addr = *saddr;
  1193. rt->rt6i_src.plen = 128;
  1194. }
  1195. #endif
  1196. }
  1197. return rt;
  1198. }
  1199. static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
  1200. {
  1201. struct fib6_info *f6i = res->f6i;
  1202. unsigned short flags = fib6_info_dst_flags(f6i);
  1203. struct net_device *dev;
  1204. struct rt6_info *pcpu_rt;
  1205. if (!fib6_info_hold_safe(f6i))
  1206. return NULL;
  1207. rcu_read_lock();
  1208. dev = ip6_rt_get_dev_rcu(res);
  1209. pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
  1210. rcu_read_unlock();
  1211. if (!pcpu_rt) {
  1212. fib6_info_release(f6i);
  1213. return NULL;
  1214. }
  1215. ip6_rt_copy_init(pcpu_rt, res);
  1216. pcpu_rt->rt6i_flags |= RTF_PCPU;
  1217. if (f6i->nh)
  1218. pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
  1219. return pcpu_rt;
  1220. }
  1221. static bool rt6_is_valid(const struct rt6_info *rt6)
  1222. {
  1223. return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
  1224. }
  1225. /* It should be called with rcu_read_lock() acquired */
  1226. static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
  1227. {
  1228. struct rt6_info *pcpu_rt;
  1229. pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
  1230. if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
  1231. struct rt6_info *prev, **p;
  1232. p = this_cpu_ptr(res->nh->rt6i_pcpu);
  1233. /* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */
  1234. prev = xchg(p, NULL);
  1235. if (prev) {
  1236. dst_dev_put(&prev->dst);
  1237. dst_release(&prev->dst);
  1238. }
  1239. pcpu_rt = NULL;
  1240. }
  1241. return pcpu_rt;
  1242. }
  1243. static struct rt6_info *rt6_make_pcpu_route(struct net *net,
  1244. const struct fib6_result *res)
  1245. {
  1246. struct rt6_info *pcpu_rt, *prev, **p;
  1247. pcpu_rt = ip6_rt_pcpu_alloc(res);
  1248. if (!pcpu_rt)
  1249. return NULL;
  1250. p = this_cpu_ptr(res->nh->rt6i_pcpu);
  1251. prev = cmpxchg(p, NULL, pcpu_rt);
  1252. BUG_ON(prev);
  1253. if (res->f6i->fib6_destroying) {
  1254. struct fib6_info *from;
  1255. from = unrcu_pointer(xchg(&pcpu_rt->from, NULL));
  1256. fib6_info_release(from);
  1257. }
  1258. return pcpu_rt;
  1259. }
  1260. /* exception hash table implementation
  1261. */
  1262. static DEFINE_SPINLOCK(rt6_exception_lock);
  1263. /* Remove rt6_ex from hash table and free the memory
  1264. * Caller must hold rt6_exception_lock
  1265. */
  1266. static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
  1267. struct rt6_exception *rt6_ex)
  1268. {
  1269. struct net *net;
  1270. if (!bucket || !rt6_ex)
  1271. return;
  1272. net = dev_net(rt6_ex->rt6i->dst.dev);
  1273. net->ipv6.rt6_stats->fib_rt_cache--;
  1274. /* purge completely the exception to allow releasing the held resources:
  1275. * some [sk] cache may keep the dst around for unlimited time
  1276. */
  1277. dst_dev_put(&rt6_ex->rt6i->dst);
  1278. hlist_del_rcu(&rt6_ex->hlist);
  1279. dst_release(&rt6_ex->rt6i->dst);
  1280. kfree_rcu(rt6_ex, rcu);
  1281. WARN_ON_ONCE(!bucket->depth);
  1282. bucket->depth--;
  1283. }
  1284. /* Remove oldest rt6_ex in bucket and free the memory
  1285. * Caller must hold rt6_exception_lock
  1286. */
  1287. static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
  1288. {
  1289. struct rt6_exception *rt6_ex, *oldest = NULL;
  1290. if (!bucket)
  1291. return;
  1292. hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
  1293. if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
  1294. oldest = rt6_ex;
  1295. }
  1296. rt6_remove_exception(bucket, oldest);
  1297. }
  1298. static u32 rt6_exception_hash(const struct in6_addr *dst,
  1299. const struct in6_addr *src)
  1300. {
  1301. static siphash_aligned_key_t rt6_exception_key;
  1302. struct {
  1303. struct in6_addr dst;
  1304. struct in6_addr src;
  1305. } __aligned(SIPHASH_ALIGNMENT) combined = {
  1306. .dst = *dst,
  1307. };
  1308. u64 val;
  1309. net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
  1310. #ifdef CONFIG_IPV6_SUBTREES
  1311. if (src)
  1312. combined.src = *src;
  1313. #endif
  1314. val = siphash(&combined, sizeof(combined), &rt6_exception_key);
  1315. return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
  1316. }
  1317. /* Helper function to find the cached rt in the hash table
  1318. * and update bucket pointer to point to the bucket for this
  1319. * (daddr, saddr) pair
  1320. * Caller must hold rt6_exception_lock
  1321. */
  1322. static struct rt6_exception *
  1323. __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
  1324. const struct in6_addr *daddr,
  1325. const struct in6_addr *saddr)
  1326. {
  1327. struct rt6_exception *rt6_ex;
  1328. u32 hval;
  1329. if (!(*bucket) || !daddr)
  1330. return NULL;
  1331. hval = rt6_exception_hash(daddr, saddr);
  1332. *bucket += hval;
  1333. hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
  1334. struct rt6_info *rt6 = rt6_ex->rt6i;
  1335. bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
  1336. #ifdef CONFIG_IPV6_SUBTREES
  1337. if (matched && saddr)
  1338. matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
  1339. #endif
  1340. if (matched)
  1341. return rt6_ex;
  1342. }
  1343. return NULL;
  1344. }
  1345. /* Helper function to find the cached rt in the hash table
  1346. * and update bucket pointer to point to the bucket for this
  1347. * (daddr, saddr) pair
  1348. * Caller must hold rcu_read_lock()
  1349. */
  1350. static struct rt6_exception *
  1351. __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
  1352. const struct in6_addr *daddr,
  1353. const struct in6_addr *saddr)
  1354. {
  1355. struct rt6_exception *rt6_ex;
  1356. u32 hval;
  1357. WARN_ON_ONCE(!rcu_read_lock_held());
  1358. if (!(*bucket) || !daddr)
  1359. return NULL;
  1360. hval = rt6_exception_hash(daddr, saddr);
  1361. *bucket += hval;
  1362. hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
  1363. struct rt6_info *rt6 = rt6_ex->rt6i;
  1364. bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
  1365. #ifdef CONFIG_IPV6_SUBTREES
  1366. if (matched && saddr)
  1367. matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
  1368. #endif
  1369. if (matched)
  1370. return rt6_ex;
  1371. }
  1372. return NULL;
  1373. }
  1374. static unsigned int fib6_mtu(const struct fib6_result *res)
  1375. {
  1376. const struct fib6_nh *nh = res->nh;
  1377. unsigned int mtu;
  1378. if (res->f6i->fib6_pmtu) {
  1379. mtu = res->f6i->fib6_pmtu;
  1380. } else {
  1381. struct net_device *dev = nh->fib_nh_dev;
  1382. struct inet6_dev *idev;
  1383. rcu_read_lock();
  1384. idev = __in6_dev_get(dev);
  1385. mtu = READ_ONCE(idev->cnf.mtu6);
  1386. rcu_read_unlock();
  1387. }
  1388. mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
  1389. return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
  1390. }
  1391. #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
  1392. /* used when the flushed bit is not relevant, only access to the bucket
  1393. * (ie., all bucket users except rt6_insert_exception);
  1394. *
  1395. * called under rcu lock; sometimes called with rt6_exception_lock held
  1396. */
  1397. static
  1398. struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
  1399. spinlock_t *lock)
  1400. {
  1401. struct rt6_exception_bucket *bucket;
  1402. if (lock)
  1403. bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
  1404. lockdep_is_held(lock));
  1405. else
  1406. bucket = rcu_dereference(nh->rt6i_exception_bucket);
  1407. /* remove bucket flushed bit if set */
  1408. if (bucket) {
  1409. unsigned long p = (unsigned long)bucket;
  1410. p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
  1411. bucket = (struct rt6_exception_bucket *)p;
  1412. }
  1413. return bucket;
  1414. }
  1415. static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
  1416. {
  1417. unsigned long p = (unsigned long)bucket;
  1418. return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
  1419. }
  1420. /* called with rt6_exception_lock held */
  1421. static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
  1422. spinlock_t *lock)
  1423. {
  1424. struct rt6_exception_bucket *bucket;
  1425. unsigned long p;
  1426. bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
  1427. lockdep_is_held(lock));
  1428. p = (unsigned long)bucket;
  1429. p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
  1430. bucket = (struct rt6_exception_bucket *)p;
  1431. rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
  1432. }
  1433. static int rt6_insert_exception(struct rt6_info *nrt,
  1434. const struct fib6_result *res)
  1435. {
  1436. struct net *net = dev_net(nrt->dst.dev);
  1437. struct rt6_exception_bucket *bucket;
  1438. struct fib6_info *f6i = res->f6i;
  1439. struct in6_addr *src_key = NULL;
  1440. struct rt6_exception *rt6_ex;
  1441. struct fib6_nh *nh = res->nh;
  1442. int max_depth;
  1443. int err = 0;
  1444. spin_lock_bh(&rt6_exception_lock);
  1445. bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
  1446. lockdep_is_held(&rt6_exception_lock));
  1447. if (!bucket) {
  1448. bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
  1449. GFP_ATOMIC);
  1450. if (!bucket) {
  1451. err = -ENOMEM;
  1452. goto out;
  1453. }
  1454. rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
  1455. } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
  1456. err = -EINVAL;
  1457. goto out;
  1458. }
  1459. #ifdef CONFIG_IPV6_SUBTREES
  1460. /* fib6_src.plen != 0 indicates f6i is in subtree
  1461. * and exception table is indexed by a hash of
  1462. * both fib6_dst and fib6_src.
  1463. * Otherwise, the exception table is indexed by
  1464. * a hash of only fib6_dst.
  1465. */
  1466. if (f6i->fib6_src.plen)
  1467. src_key = &nrt->rt6i_src.addr;
  1468. #endif
  1469. /* rt6_mtu_change() might lower mtu on f6i.
  1470. * Only insert this exception route if its mtu
  1471. * is less than f6i's mtu value.
  1472. */
  1473. if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
  1474. err = -EINVAL;
  1475. goto out;
  1476. }
  1477. rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
  1478. src_key);
  1479. if (rt6_ex)
  1480. rt6_remove_exception(bucket, rt6_ex);
  1481. rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
  1482. if (!rt6_ex) {
  1483. err = -ENOMEM;
  1484. goto out;
  1485. }
  1486. rt6_ex->rt6i = nrt;
  1487. rt6_ex->stamp = jiffies;
  1488. hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
  1489. bucket->depth++;
  1490. net->ipv6.rt6_stats->fib_rt_cache++;
  1491. /* Randomize max depth to avoid some side channels attacks. */
  1492. max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH);
  1493. while (bucket->depth > max_depth)
  1494. rt6_exception_remove_oldest(bucket);
  1495. out:
  1496. spin_unlock_bh(&rt6_exception_lock);
  1497. /* Update fn->fn_sernum to invalidate all cached dst */
  1498. if (!err) {
  1499. spin_lock_bh(&f6i->fib6_table->tb6_lock);
  1500. fib6_update_sernum(net, f6i);
  1501. fib6_add_gc_list(f6i);
  1502. spin_unlock_bh(&f6i->fib6_table->tb6_lock);
  1503. fib6_force_start_gc(net);
  1504. }
  1505. return err;
  1506. }
  1507. static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
  1508. {
  1509. struct rt6_exception_bucket *bucket;
  1510. struct rt6_exception *rt6_ex;
  1511. struct hlist_node *tmp;
  1512. int i;
  1513. spin_lock_bh(&rt6_exception_lock);
  1514. bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
  1515. if (!bucket)
  1516. goto out;
  1517. /* Prevent rt6_insert_exception() to recreate the bucket list */
  1518. if (!from)
  1519. fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
  1520. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1521. hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
  1522. if (!from ||
  1523. rcu_access_pointer(rt6_ex->rt6i->from) == from)
  1524. rt6_remove_exception(bucket, rt6_ex);
  1525. }
  1526. WARN_ON_ONCE(!from && bucket->depth);
  1527. bucket++;
  1528. }
  1529. out:
  1530. spin_unlock_bh(&rt6_exception_lock);
  1531. }
  1532. static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
  1533. {
  1534. struct fib6_info *f6i = arg;
  1535. fib6_nh_flush_exceptions(nh, f6i);
  1536. return 0;
  1537. }
  1538. void rt6_flush_exceptions(struct fib6_info *f6i)
  1539. {
  1540. if (f6i->nh)
  1541. nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
  1542. f6i);
  1543. else
  1544. fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
  1545. }
  1546. /* Find cached rt in the hash table inside passed in rt
  1547. * Caller has to hold rcu_read_lock()
  1548. */
  1549. static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
  1550. const struct in6_addr *daddr,
  1551. const struct in6_addr *saddr)
  1552. {
  1553. const struct in6_addr *src_key = NULL;
  1554. struct rt6_exception_bucket *bucket;
  1555. struct rt6_exception *rt6_ex;
  1556. struct rt6_info *ret = NULL;
  1557. #ifdef CONFIG_IPV6_SUBTREES
  1558. /* fib6i_src.plen != 0 indicates f6i is in subtree
  1559. * and exception table is indexed by a hash of
  1560. * both fib6_dst and fib6_src.
  1561. * However, the src addr used to create the hash
  1562. * might not be exactly the passed in saddr which
  1563. * is a /128 addr from the flow.
  1564. * So we need to use f6i->fib6_src to redo lookup
  1565. * if the passed in saddr does not find anything.
  1566. * (See the logic in ip6_rt_cache_alloc() on how
  1567. * rt->rt6i_src is updated.)
  1568. */
  1569. if (res->f6i->fib6_src.plen)
  1570. src_key = saddr;
  1571. find_ex:
  1572. #endif
  1573. bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
  1574. rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
  1575. if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
  1576. ret = rt6_ex->rt6i;
  1577. #ifdef CONFIG_IPV6_SUBTREES
  1578. /* Use fib6_src as src_key and redo lookup */
  1579. if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
  1580. src_key = &res->f6i->fib6_src.addr;
  1581. goto find_ex;
  1582. }
  1583. #endif
  1584. return ret;
  1585. }
  1586. /* Remove the passed in cached rt from the hash table that contains it */
  1587. static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
  1588. const struct rt6_info *rt)
  1589. {
  1590. const struct in6_addr *src_key = NULL;
  1591. struct rt6_exception_bucket *bucket;
  1592. struct rt6_exception *rt6_ex;
  1593. int err;
  1594. if (!rcu_access_pointer(nh->rt6i_exception_bucket))
  1595. return -ENOENT;
  1596. spin_lock_bh(&rt6_exception_lock);
  1597. bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
  1598. #ifdef CONFIG_IPV6_SUBTREES
  1599. /* rt6i_src.plen != 0 indicates 'from' is in subtree
  1600. * and exception table is indexed by a hash of
  1601. * both rt6i_dst and rt6i_src.
  1602. * Otherwise, the exception table is indexed by
  1603. * a hash of only rt6i_dst.
  1604. */
  1605. if (plen)
  1606. src_key = &rt->rt6i_src.addr;
  1607. #endif
  1608. rt6_ex = __rt6_find_exception_spinlock(&bucket,
  1609. &rt->rt6i_dst.addr,
  1610. src_key);
  1611. if (rt6_ex) {
  1612. rt6_remove_exception(bucket, rt6_ex);
  1613. err = 0;
  1614. } else {
  1615. err = -ENOENT;
  1616. }
  1617. spin_unlock_bh(&rt6_exception_lock);
  1618. return err;
  1619. }
  1620. struct fib6_nh_excptn_arg {
  1621. struct rt6_info *rt;
  1622. int plen;
  1623. };
  1624. static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
  1625. {
  1626. struct fib6_nh_excptn_arg *arg = _arg;
  1627. int err;
  1628. err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
  1629. if (err == 0)
  1630. return 1;
  1631. return 0;
  1632. }
  1633. static int rt6_remove_exception_rt(struct rt6_info *rt)
  1634. {
  1635. struct fib6_info *from;
  1636. from = rcu_dereference(rt->from);
  1637. if (!from || !(rt->rt6i_flags & RTF_CACHE))
  1638. return -EINVAL;
  1639. if (from->nh) {
  1640. struct fib6_nh_excptn_arg arg = {
  1641. .rt = rt,
  1642. .plen = from->fib6_src.plen
  1643. };
  1644. int rc;
  1645. /* rc = 1 means an entry was found */
  1646. rc = nexthop_for_each_fib6_nh(from->nh,
  1647. rt6_nh_remove_exception_rt,
  1648. &arg);
  1649. return rc ? 0 : -ENOENT;
  1650. }
  1651. return fib6_nh_remove_exception(from->fib6_nh,
  1652. from->fib6_src.plen, rt);
  1653. }
  1654. /* Find rt6_ex which contains the passed in rt cache and
  1655. * refresh its stamp
  1656. */
  1657. static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
  1658. const struct rt6_info *rt)
  1659. {
  1660. const struct in6_addr *src_key = NULL;
  1661. struct rt6_exception_bucket *bucket;
  1662. struct rt6_exception *rt6_ex;
  1663. bucket = fib6_nh_get_excptn_bucket(nh, NULL);
  1664. #ifdef CONFIG_IPV6_SUBTREES
  1665. /* rt6i_src.plen != 0 indicates 'from' is in subtree
  1666. * and exception table is indexed by a hash of
  1667. * both rt6i_dst and rt6i_src.
  1668. * Otherwise, the exception table is indexed by
  1669. * a hash of only rt6i_dst.
  1670. */
  1671. if (plen)
  1672. src_key = &rt->rt6i_src.addr;
  1673. #endif
  1674. rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
  1675. if (rt6_ex)
  1676. rt6_ex->stamp = jiffies;
  1677. }
  1678. struct fib6_nh_match_arg {
  1679. const struct net_device *dev;
  1680. const struct in6_addr *gw;
  1681. struct fib6_nh *match;
  1682. };
  1683. /* determine if fib6_nh has given device and gateway */
  1684. static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
  1685. {
  1686. struct fib6_nh_match_arg *arg = _arg;
  1687. if (arg->dev != nh->fib_nh_dev ||
  1688. (arg->gw && !nh->fib_nh_gw_family) ||
  1689. (!arg->gw && nh->fib_nh_gw_family) ||
  1690. (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
  1691. return 0;
  1692. arg->match = nh;
  1693. /* found a match, break the loop */
  1694. return 1;
  1695. }
  1696. static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
  1697. {
  1698. struct fib6_info *from;
  1699. struct fib6_nh *fib6_nh;
  1700. rcu_read_lock();
  1701. from = rcu_dereference(rt->from);
  1702. if (!from || !(rt->rt6i_flags & RTF_CACHE))
  1703. goto unlock;
  1704. if (from->nh) {
  1705. struct fib6_nh_match_arg arg = {
  1706. .dev = rt->dst.dev,
  1707. .gw = &rt->rt6i_gateway,
  1708. };
  1709. nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
  1710. if (!arg.match)
  1711. goto unlock;
  1712. fib6_nh = arg.match;
  1713. } else {
  1714. fib6_nh = from->fib6_nh;
  1715. }
  1716. fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
  1717. unlock:
  1718. rcu_read_unlock();
  1719. }
  1720. static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
  1721. struct rt6_info *rt, int mtu)
  1722. {
  1723. /* If the new MTU is lower than the route PMTU, this new MTU will be the
  1724. * lowest MTU in the path: always allow updating the route PMTU to
  1725. * reflect PMTU decreases.
  1726. *
  1727. * If the new MTU is higher, and the route PMTU is equal to the local
  1728. * MTU, this means the old MTU is the lowest in the path, so allow
  1729. * updating it: if other nodes now have lower MTUs, PMTU discovery will
  1730. * handle this.
  1731. */
  1732. if (dst_mtu(&rt->dst) >= mtu)
  1733. return true;
  1734. if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
  1735. return true;
  1736. return false;
  1737. }
  1738. static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
  1739. const struct fib6_nh *nh, int mtu)
  1740. {
  1741. struct rt6_exception_bucket *bucket;
  1742. struct rt6_exception *rt6_ex;
  1743. int i;
  1744. bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
  1745. if (!bucket)
  1746. return;
  1747. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1748. hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
  1749. struct rt6_info *entry = rt6_ex->rt6i;
  1750. /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
  1751. * route), the metrics of its rt->from have already
  1752. * been updated.
  1753. */
  1754. if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
  1755. rt6_mtu_change_route_allowed(idev, entry, mtu))
  1756. dst_metric_set(&entry->dst, RTAX_MTU, mtu);
  1757. }
  1758. bucket++;
  1759. }
  1760. }
  1761. #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
  1762. static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
  1763. const struct in6_addr *gateway)
  1764. {
  1765. struct rt6_exception_bucket *bucket;
  1766. struct rt6_exception *rt6_ex;
  1767. struct hlist_node *tmp;
  1768. int i;
  1769. if (!rcu_access_pointer(nh->rt6i_exception_bucket))
  1770. return;
  1771. spin_lock_bh(&rt6_exception_lock);
  1772. bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
  1773. if (bucket) {
  1774. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1775. hlist_for_each_entry_safe(rt6_ex, tmp,
  1776. &bucket->chain, hlist) {
  1777. struct rt6_info *entry = rt6_ex->rt6i;
  1778. if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
  1779. RTF_CACHE_GATEWAY &&
  1780. ipv6_addr_equal(gateway,
  1781. &entry->rt6i_gateway)) {
  1782. rt6_remove_exception(bucket, rt6_ex);
  1783. }
  1784. }
  1785. bucket++;
  1786. }
  1787. }
  1788. spin_unlock_bh(&rt6_exception_lock);
  1789. }
  1790. static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
  1791. struct rt6_exception *rt6_ex,
  1792. struct fib6_gc_args *gc_args,
  1793. unsigned long now)
  1794. {
  1795. struct rt6_info *rt = rt6_ex->rt6i;
  1796. /* we are pruning and obsoleting aged-out and non gateway exceptions
  1797. * even if others have still references to them, so that on next
  1798. * dst_check() such references can be dropped.
  1799. * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
  1800. * expired, independently from their aging, as per RFC 8201 section 4
  1801. */
  1802. if (!(rt->rt6i_flags & RTF_EXPIRES)) {
  1803. if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
  1804. pr_debug("aging clone %p\n", rt);
  1805. rt6_remove_exception(bucket, rt6_ex);
  1806. return;
  1807. }
  1808. } else if (time_after(jiffies, rt->dst.expires)) {
  1809. pr_debug("purging expired route %p\n", rt);
  1810. rt6_remove_exception(bucket, rt6_ex);
  1811. return;
  1812. }
  1813. if (rt->rt6i_flags & RTF_GATEWAY) {
  1814. struct neighbour *neigh;
  1815. neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
  1816. if (!(neigh && (neigh->flags & NTF_ROUTER))) {
  1817. pr_debug("purging route %p via non-router but gateway\n",
  1818. rt);
  1819. rt6_remove_exception(bucket, rt6_ex);
  1820. return;
  1821. }
  1822. }
  1823. gc_args->more++;
  1824. }
  1825. static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
  1826. struct fib6_gc_args *gc_args,
  1827. unsigned long now)
  1828. {
  1829. struct rt6_exception_bucket *bucket;
  1830. struct rt6_exception *rt6_ex;
  1831. struct hlist_node *tmp;
  1832. int i;
  1833. if (!rcu_access_pointer(nh->rt6i_exception_bucket))
  1834. return;
  1835. rcu_read_lock_bh();
  1836. spin_lock(&rt6_exception_lock);
  1837. bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
  1838. if (bucket) {
  1839. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1840. hlist_for_each_entry_safe(rt6_ex, tmp,
  1841. &bucket->chain, hlist) {
  1842. rt6_age_examine_exception(bucket, rt6_ex,
  1843. gc_args, now);
  1844. }
  1845. bucket++;
  1846. }
  1847. }
  1848. spin_unlock(&rt6_exception_lock);
  1849. rcu_read_unlock_bh();
  1850. }
  1851. struct fib6_nh_age_excptn_arg {
  1852. struct fib6_gc_args *gc_args;
  1853. unsigned long now;
  1854. };
  1855. static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
  1856. {
  1857. struct fib6_nh_age_excptn_arg *arg = _arg;
  1858. fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
  1859. return 0;
  1860. }
  1861. void rt6_age_exceptions(struct fib6_info *f6i,
  1862. struct fib6_gc_args *gc_args,
  1863. unsigned long now)
  1864. {
  1865. if (f6i->nh) {
  1866. struct fib6_nh_age_excptn_arg arg = {
  1867. .gc_args = gc_args,
  1868. .now = now
  1869. };
  1870. nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
  1871. &arg);
  1872. } else {
  1873. fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
  1874. }
  1875. }
  1876. /* must be called with rcu lock held */
  1877. int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
  1878. struct flowi6 *fl6, struct fib6_result *res, int strict)
  1879. {
  1880. struct fib6_node *fn, *saved_fn;
  1881. fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  1882. saved_fn = fn;
  1883. redo_rt6_select:
  1884. rt6_select(net, fn, oif, res, strict);
  1885. if (res->f6i == net->ipv6.fib6_null_entry) {
  1886. fn = fib6_backtrack(fn, &fl6->saddr);
  1887. if (fn)
  1888. goto redo_rt6_select;
  1889. else if (strict & RT6_LOOKUP_F_REACHABLE) {
  1890. /* also consider unreachable route */
  1891. strict &= ~RT6_LOOKUP_F_REACHABLE;
  1892. fn = saved_fn;
  1893. goto redo_rt6_select;
  1894. }
  1895. }
  1896. trace_fib6_table_lookup(net, res, table, fl6);
  1897. return 0;
  1898. }
  1899. struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
  1900. int oif, struct flowi6 *fl6,
  1901. const struct sk_buff *skb, int flags)
  1902. {
  1903. struct fib6_result res = {};
  1904. struct rt6_info *rt = NULL;
  1905. int strict = 0;
  1906. WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
  1907. !rcu_read_lock_held());
  1908. strict |= flags & RT6_LOOKUP_F_IFACE;
  1909. strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
  1910. if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
  1911. strict |= RT6_LOOKUP_F_REACHABLE;
  1912. rcu_read_lock();
  1913. fib6_table_lookup(net, table, oif, fl6, &res, strict);
  1914. if (res.f6i == net->ipv6.fib6_null_entry)
  1915. goto out;
  1916. fib6_select_path(net, &res, fl6, oif, false, skb, strict);
  1917. /*Search through exception table */
  1918. rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
  1919. if (rt) {
  1920. goto out;
  1921. } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
  1922. !res.nh->fib_nh_gw_family)) {
  1923. /* Create a RTF_CACHE clone which will not be
  1924. * owned by the fib6 tree. It is for the special case where
  1925. * the daddr in the skb during the neighbor look-up is different
  1926. * from the fl6->daddr used to look-up route here.
  1927. */
  1928. rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
  1929. if (rt) {
  1930. /* 1 refcnt is taken during ip6_rt_cache_alloc().
  1931. * As rt6_uncached_list_add() does not consume refcnt,
  1932. * this refcnt is always returned to the caller even
  1933. * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
  1934. */
  1935. rt6_uncached_list_add(rt);
  1936. rcu_read_unlock();
  1937. return rt;
  1938. }
  1939. } else {
  1940. /* Get a percpu copy */
  1941. local_bh_disable();
  1942. rt = rt6_get_pcpu_route(&res);
  1943. if (!rt)
  1944. rt = rt6_make_pcpu_route(net, &res);
  1945. local_bh_enable();
  1946. }
  1947. out:
  1948. if (!rt)
  1949. rt = net->ipv6.ip6_null_entry;
  1950. if (!(flags & RT6_LOOKUP_F_DST_NOREF))
  1951. ip6_hold_safe(net, &rt);
  1952. rcu_read_unlock();
  1953. return rt;
  1954. }
  1955. EXPORT_SYMBOL_GPL(ip6_pol_route);
  1956. INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
  1957. struct fib6_table *table,
  1958. struct flowi6 *fl6,
  1959. const struct sk_buff *skb,
  1960. int flags)
  1961. {
  1962. return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
  1963. }
  1964. struct dst_entry *ip6_route_input_lookup(struct net *net,
  1965. struct net_device *dev,
  1966. struct flowi6 *fl6,
  1967. const struct sk_buff *skb,
  1968. int flags)
  1969. {
  1970. if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
  1971. flags |= RT6_LOOKUP_F_IFACE;
  1972. return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
  1973. }
  1974. EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
  1975. static void ip6_multipath_l3_keys(const struct sk_buff *skb,
  1976. struct flow_keys *keys,
  1977. struct flow_keys *flkeys)
  1978. {
  1979. const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
  1980. const struct ipv6hdr *key_iph = outer_iph;
  1981. struct flow_keys *_flkeys = flkeys;
  1982. const struct ipv6hdr *inner_iph;
  1983. const struct icmp6hdr *icmph;
  1984. struct ipv6hdr _inner_iph;
  1985. struct icmp6hdr _icmph;
  1986. if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
  1987. goto out;
  1988. icmph = skb_header_pointer(skb, skb_transport_offset(skb),
  1989. sizeof(_icmph), &_icmph);
  1990. if (!icmph)
  1991. goto out;
  1992. if (!icmpv6_is_err(icmph->icmp6_type))
  1993. goto out;
  1994. inner_iph = skb_header_pointer(skb,
  1995. skb_transport_offset(skb) + sizeof(*icmph),
  1996. sizeof(_inner_iph), &_inner_iph);
  1997. if (!inner_iph)
  1998. goto out;
  1999. key_iph = inner_iph;
  2000. _flkeys = NULL;
  2001. out:
  2002. if (_flkeys) {
  2003. keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
  2004. keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
  2005. keys->tags.flow_label = _flkeys->tags.flow_label;
  2006. keys->basic.ip_proto = _flkeys->basic.ip_proto;
  2007. } else {
  2008. keys->addrs.v6addrs.src = key_iph->saddr;
  2009. keys->addrs.v6addrs.dst = key_iph->daddr;
  2010. keys->tags.flow_label = ip6_flowlabel(key_iph);
  2011. keys->basic.ip_proto = key_iph->nexthdr;
  2012. }
  2013. }
  2014. static u32 rt6_multipath_custom_hash_outer(const struct net *net,
  2015. const struct sk_buff *skb,
  2016. bool *p_has_inner)
  2017. {
  2018. u32 hash_fields = ip6_multipath_hash_fields(net);
  2019. struct flow_keys keys, hash_keys;
  2020. if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
  2021. return 0;
  2022. memset(&hash_keys, 0, sizeof(hash_keys));
  2023. skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
  2024. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2025. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
  2026. hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
  2027. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
  2028. hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
  2029. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
  2030. hash_keys.basic.ip_proto = keys.basic.ip_proto;
  2031. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
  2032. hash_keys.tags.flow_label = keys.tags.flow_label;
  2033. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
  2034. hash_keys.ports.src = keys.ports.src;
  2035. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
  2036. hash_keys.ports.dst = keys.ports.dst;
  2037. *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
  2038. return fib_multipath_hash_from_keys(net, &hash_keys);
  2039. }
  2040. static u32 rt6_multipath_custom_hash_inner(const struct net *net,
  2041. const struct sk_buff *skb,
  2042. bool has_inner)
  2043. {
  2044. u32 hash_fields = ip6_multipath_hash_fields(net);
  2045. struct flow_keys keys, hash_keys;
  2046. /* We assume the packet carries an encapsulation, but if none was
  2047. * encountered during dissection of the outer flow, then there is no
  2048. * point in calling the flow dissector again.
  2049. */
  2050. if (!has_inner)
  2051. return 0;
  2052. if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
  2053. return 0;
  2054. memset(&hash_keys, 0, sizeof(hash_keys));
  2055. skb_flow_dissect_flow_keys(skb, &keys, 0);
  2056. if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
  2057. return 0;
  2058. if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  2059. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  2060. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
  2061. hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
  2062. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
  2063. hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
  2064. } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  2065. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2066. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
  2067. hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
  2068. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
  2069. hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
  2070. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
  2071. hash_keys.tags.flow_label = keys.tags.flow_label;
  2072. }
  2073. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
  2074. hash_keys.basic.ip_proto = keys.basic.ip_proto;
  2075. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
  2076. hash_keys.ports.src = keys.ports.src;
  2077. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
  2078. hash_keys.ports.dst = keys.ports.dst;
  2079. return fib_multipath_hash_from_keys(net, &hash_keys);
  2080. }
  2081. static u32 rt6_multipath_custom_hash_skb(const struct net *net,
  2082. const struct sk_buff *skb)
  2083. {
  2084. u32 mhash, mhash_inner;
  2085. bool has_inner = true;
  2086. mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
  2087. mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
  2088. return jhash_2words(mhash, mhash_inner, 0);
  2089. }
  2090. static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
  2091. const struct flowi6 *fl6)
  2092. {
  2093. u32 hash_fields = ip6_multipath_hash_fields(net);
  2094. struct flow_keys hash_keys;
  2095. if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
  2096. return 0;
  2097. memset(&hash_keys, 0, sizeof(hash_keys));
  2098. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2099. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
  2100. hash_keys.addrs.v6addrs.src = fl6->saddr;
  2101. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
  2102. hash_keys.addrs.v6addrs.dst = fl6->daddr;
  2103. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
  2104. hash_keys.basic.ip_proto = fl6->flowi6_proto;
  2105. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
  2106. hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
  2107. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
  2108. hash_keys.ports.src = fl6->fl6_sport;
  2109. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
  2110. hash_keys.ports.dst = fl6->fl6_dport;
  2111. return fib_multipath_hash_from_keys(net, &hash_keys);
  2112. }
  2113. /* if skb is set it will be used and fl6 can be NULL */
  2114. u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
  2115. const struct sk_buff *skb, struct flow_keys *flkeys)
  2116. {
  2117. struct flow_keys hash_keys;
  2118. u32 mhash = 0;
  2119. switch (ip6_multipath_hash_policy(net)) {
  2120. case 0:
  2121. memset(&hash_keys, 0, sizeof(hash_keys));
  2122. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2123. if (skb) {
  2124. ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
  2125. } else {
  2126. hash_keys.addrs.v6addrs.src = fl6->saddr;
  2127. hash_keys.addrs.v6addrs.dst = fl6->daddr;
  2128. hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
  2129. hash_keys.basic.ip_proto = fl6->flowi6_proto;
  2130. }
  2131. mhash = fib_multipath_hash_from_keys(net, &hash_keys);
  2132. break;
  2133. case 1:
  2134. if (skb) {
  2135. unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
  2136. struct flow_keys keys;
  2137. /* short-circuit if we already have L4 hash present */
  2138. if (skb->l4_hash)
  2139. return skb_get_hash_raw(skb) >> 1;
  2140. memset(&hash_keys, 0, sizeof(hash_keys));
  2141. if (!flkeys) {
  2142. skb_flow_dissect_flow_keys(skb, &keys, flag);
  2143. flkeys = &keys;
  2144. }
  2145. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2146. hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
  2147. hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
  2148. hash_keys.ports.src = flkeys->ports.src;
  2149. hash_keys.ports.dst = flkeys->ports.dst;
  2150. hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
  2151. } else {
  2152. memset(&hash_keys, 0, sizeof(hash_keys));
  2153. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2154. hash_keys.addrs.v6addrs.src = fl6->saddr;
  2155. hash_keys.addrs.v6addrs.dst = fl6->daddr;
  2156. hash_keys.ports.src = fl6->fl6_sport;
  2157. hash_keys.ports.dst = fl6->fl6_dport;
  2158. hash_keys.basic.ip_proto = fl6->flowi6_proto;
  2159. }
  2160. mhash = fib_multipath_hash_from_keys(net, &hash_keys);
  2161. break;
  2162. case 2:
  2163. memset(&hash_keys, 0, sizeof(hash_keys));
  2164. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2165. if (skb) {
  2166. struct flow_keys keys;
  2167. if (!flkeys) {
  2168. skb_flow_dissect_flow_keys(skb, &keys, 0);
  2169. flkeys = &keys;
  2170. }
  2171. /* Inner can be v4 or v6 */
  2172. if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  2173. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  2174. hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
  2175. hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
  2176. } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  2177. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2178. hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
  2179. hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
  2180. hash_keys.tags.flow_label = flkeys->tags.flow_label;
  2181. hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
  2182. } else {
  2183. /* Same as case 0 */
  2184. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2185. ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
  2186. }
  2187. } else {
  2188. /* Same as case 0 */
  2189. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  2190. hash_keys.addrs.v6addrs.src = fl6->saddr;
  2191. hash_keys.addrs.v6addrs.dst = fl6->daddr;
  2192. hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
  2193. hash_keys.basic.ip_proto = fl6->flowi6_proto;
  2194. }
  2195. mhash = fib_multipath_hash_from_keys(net, &hash_keys);
  2196. break;
  2197. case 3:
  2198. if (skb)
  2199. mhash = rt6_multipath_custom_hash_skb(net, skb);
  2200. else
  2201. mhash = rt6_multipath_custom_hash_fl6(net, fl6);
  2202. break;
  2203. }
  2204. return mhash >> 1;
  2205. }
  2206. /* Called with rcu held */
  2207. void ip6_route_input(struct sk_buff *skb)
  2208. {
  2209. const struct ipv6hdr *iph = ipv6_hdr(skb);
  2210. struct net *net = dev_net(skb->dev);
  2211. int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
  2212. struct ip_tunnel_info *tun_info;
  2213. struct flowi6 fl6 = {
  2214. .flowi6_iif = skb->dev->ifindex,
  2215. .daddr = iph->daddr,
  2216. .saddr = iph->saddr,
  2217. .flowlabel = ip6_flowinfo(iph),
  2218. .flowi6_mark = skb->mark,
  2219. .flowi6_proto = iph->nexthdr,
  2220. };
  2221. struct flow_keys *flkeys = NULL, _flkeys;
  2222. tun_info = skb_tunnel_info(skb);
  2223. if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
  2224. fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
  2225. if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
  2226. flkeys = &_flkeys;
  2227. if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
  2228. fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
  2229. skb_dst_drop(skb);
  2230. skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
  2231. &fl6, skb, flags));
  2232. }
  2233. INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
  2234. struct fib6_table *table,
  2235. struct flowi6 *fl6,
  2236. const struct sk_buff *skb,
  2237. int flags)
  2238. {
  2239. return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
  2240. }
  2241. static struct dst_entry *ip6_route_output_flags_noref(struct net *net,
  2242. const struct sock *sk,
  2243. struct flowi6 *fl6,
  2244. int flags)
  2245. {
  2246. bool any_src;
  2247. if (ipv6_addr_type(&fl6->daddr) &
  2248. (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
  2249. struct dst_entry *dst;
  2250. /* This function does not take refcnt on the dst */
  2251. dst = l3mdev_link_scope_lookup(net, fl6);
  2252. if (dst)
  2253. return dst;
  2254. }
  2255. fl6->flowi6_iif = LOOPBACK_IFINDEX;
  2256. flags |= RT6_LOOKUP_F_DST_NOREF;
  2257. any_src = ipv6_addr_any(&fl6->saddr);
  2258. if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
  2259. (fl6->flowi6_oif && any_src))
  2260. flags |= RT6_LOOKUP_F_IFACE;
  2261. if (!any_src)
  2262. flags |= RT6_LOOKUP_F_HAS_SADDR;
  2263. else if (sk)
  2264. flags |= rt6_srcprefs2flags(READ_ONCE(inet6_sk(sk)->srcprefs));
  2265. return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
  2266. }
  2267. struct dst_entry *ip6_route_output_flags(struct net *net,
  2268. const struct sock *sk,
  2269. struct flowi6 *fl6,
  2270. int flags)
  2271. {
  2272. struct dst_entry *dst;
  2273. struct rt6_info *rt6;
  2274. rcu_read_lock();
  2275. dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
  2276. rt6 = dst_rt6_info(dst);
  2277. /* For dst cached in uncached_list, refcnt is already taken. */
  2278. if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) {
  2279. dst = &net->ipv6.ip6_null_entry->dst;
  2280. dst_hold(dst);
  2281. }
  2282. rcu_read_unlock();
  2283. return dst;
  2284. }
  2285. EXPORT_SYMBOL_GPL(ip6_route_output_flags);
  2286. struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
  2287. {
  2288. struct rt6_info *rt, *ort = dst_rt6_info(dst_orig);
  2289. struct net_device *loopback_dev = net->loopback_dev;
  2290. struct dst_entry *new = NULL;
  2291. rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev,
  2292. DST_OBSOLETE_DEAD, 0);
  2293. if (rt) {
  2294. rt6_info_init(rt);
  2295. atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
  2296. new = &rt->dst;
  2297. new->__use = 1;
  2298. new->input = dst_discard;
  2299. new->output = dst_discard_out;
  2300. dst_copy_metrics(new, &ort->dst);
  2301. rt->rt6i_idev = in6_dev_get(loopback_dev);
  2302. rt->rt6i_gateway = ort->rt6i_gateway;
  2303. rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
  2304. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  2305. #ifdef CONFIG_IPV6_SUBTREES
  2306. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  2307. #endif
  2308. }
  2309. dst_release(dst_orig);
  2310. return new ? new : ERR_PTR(-ENOMEM);
  2311. }
  2312. /*
  2313. * Destination cache support functions
  2314. */
  2315. static bool fib6_check(struct fib6_info *f6i, u32 cookie)
  2316. {
  2317. u32 rt_cookie = 0;
  2318. if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
  2319. return false;
  2320. if (fib6_check_expired(f6i))
  2321. return false;
  2322. return true;
  2323. }
  2324. static struct dst_entry *rt6_check(struct rt6_info *rt,
  2325. struct fib6_info *from,
  2326. u32 cookie)
  2327. {
  2328. u32 rt_cookie = 0;
  2329. if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
  2330. rt_cookie != cookie)
  2331. return NULL;
  2332. if (rt6_check_expired(rt))
  2333. return NULL;
  2334. return &rt->dst;
  2335. }
  2336. static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
  2337. struct fib6_info *from,
  2338. u32 cookie)
  2339. {
  2340. if (!__rt6_check_expired(rt) &&
  2341. rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
  2342. fib6_check(from, cookie))
  2343. return &rt->dst;
  2344. else
  2345. return NULL;
  2346. }
  2347. INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
  2348. u32 cookie)
  2349. {
  2350. struct dst_entry *dst_ret;
  2351. struct fib6_info *from;
  2352. struct rt6_info *rt;
  2353. rt = dst_rt6_info(dst);
  2354. if (rt->sernum)
  2355. return rt6_is_valid(rt) ? dst : NULL;
  2356. rcu_read_lock();
  2357. /* All IPV6 dsts are created with ->obsolete set to the value
  2358. * DST_OBSOLETE_FORCE_CHK which forces validation calls down
  2359. * into this function always.
  2360. */
  2361. from = rcu_dereference(rt->from);
  2362. if (from && (rt->rt6i_flags & RTF_PCPU ||
  2363. unlikely(!list_empty(&rt->dst.rt_uncached))))
  2364. dst_ret = rt6_dst_from_check(rt, from, cookie);
  2365. else
  2366. dst_ret = rt6_check(rt, from, cookie);
  2367. rcu_read_unlock();
  2368. return dst_ret;
  2369. }
  2370. EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
  2371. static void ip6_negative_advice(struct sock *sk,
  2372. struct dst_entry *dst)
  2373. {
  2374. struct rt6_info *rt = dst_rt6_info(dst);
  2375. if (rt->rt6i_flags & RTF_CACHE) {
  2376. rcu_read_lock();
  2377. if (rt6_check_expired(rt)) {
  2378. /* rt/dst can not be destroyed yet,
  2379. * because of rcu_read_lock()
  2380. */
  2381. sk_dst_reset(sk);
  2382. rt6_remove_exception_rt(rt);
  2383. }
  2384. rcu_read_unlock();
  2385. return;
  2386. }
  2387. sk_dst_reset(sk);
  2388. }
  2389. static void ip6_link_failure(struct sk_buff *skb)
  2390. {
  2391. struct rt6_info *rt;
  2392. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
  2393. rt = dst_rt6_info(skb_dst(skb));
  2394. if (rt) {
  2395. rcu_read_lock();
  2396. if (rt->rt6i_flags & RTF_CACHE) {
  2397. rt6_remove_exception_rt(rt);
  2398. } else {
  2399. struct fib6_info *from;
  2400. struct fib6_node *fn;
  2401. from = rcu_dereference(rt->from);
  2402. if (from) {
  2403. fn = rcu_dereference(from->fib6_node);
  2404. if (fn && (rt->rt6i_flags & RTF_DEFAULT))
  2405. WRITE_ONCE(fn->fn_sernum, -1);
  2406. }
  2407. }
  2408. rcu_read_unlock();
  2409. }
  2410. }
  2411. static void rt6_update_expires(struct rt6_info *rt0, int timeout)
  2412. {
  2413. if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
  2414. struct fib6_info *from;
  2415. rcu_read_lock();
  2416. from = rcu_dereference(rt0->from);
  2417. if (from)
  2418. rt0->dst.expires = from->expires;
  2419. rcu_read_unlock();
  2420. }
  2421. dst_set_expires(&rt0->dst, timeout);
  2422. rt0->rt6i_flags |= RTF_EXPIRES;
  2423. }
  2424. static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
  2425. {
  2426. struct net *net = dev_net(rt->dst.dev);
  2427. dst_metric_set(&rt->dst, RTAX_MTU, mtu);
  2428. rt->rt6i_flags |= RTF_MODIFIED;
  2429. rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
  2430. }
  2431. static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
  2432. {
  2433. return !(rt->rt6i_flags & RTF_CACHE) &&
  2434. (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
  2435. }
  2436. static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
  2437. const struct ipv6hdr *iph, u32 mtu,
  2438. bool confirm_neigh)
  2439. {
  2440. const struct in6_addr *daddr, *saddr;
  2441. struct rt6_info *rt6 = dst_rt6_info(dst);
  2442. /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
  2443. * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
  2444. * [see also comment in rt6_mtu_change_route()]
  2445. */
  2446. if (iph) {
  2447. daddr = &iph->daddr;
  2448. saddr = &iph->saddr;
  2449. } else if (sk) {
  2450. daddr = &sk->sk_v6_daddr;
  2451. saddr = &inet6_sk(sk)->saddr;
  2452. } else {
  2453. daddr = NULL;
  2454. saddr = NULL;
  2455. }
  2456. if (confirm_neigh)
  2457. dst_confirm_neigh(dst, daddr);
  2458. if (mtu < IPV6_MIN_MTU)
  2459. return;
  2460. if (mtu >= dst_mtu(dst))
  2461. return;
  2462. if (!rt6_cache_allowed_for_pmtu(rt6)) {
  2463. rt6_do_update_pmtu(rt6, mtu);
  2464. /* update rt6_ex->stamp for cache */
  2465. if (rt6->rt6i_flags & RTF_CACHE)
  2466. rt6_update_exception_stamp_rt(rt6);
  2467. } else if (daddr) {
  2468. struct fib6_result res = {};
  2469. struct rt6_info *nrt6;
  2470. rcu_read_lock();
  2471. res.f6i = rcu_dereference(rt6->from);
  2472. if (!res.f6i)
  2473. goto out_unlock;
  2474. res.fib6_flags = res.f6i->fib6_flags;
  2475. res.fib6_type = res.f6i->fib6_type;
  2476. if (res.f6i->nh) {
  2477. struct fib6_nh_match_arg arg = {
  2478. .dev = dst->dev,
  2479. .gw = &rt6->rt6i_gateway,
  2480. };
  2481. nexthop_for_each_fib6_nh(res.f6i->nh,
  2482. fib6_nh_find_match, &arg);
  2483. /* fib6_info uses a nexthop that does not have fib6_nh
  2484. * using the dst->dev + gw. Should be impossible.
  2485. */
  2486. if (!arg.match)
  2487. goto out_unlock;
  2488. res.nh = arg.match;
  2489. } else {
  2490. res.nh = res.f6i->fib6_nh;
  2491. }
  2492. nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
  2493. if (nrt6) {
  2494. rt6_do_update_pmtu(nrt6, mtu);
  2495. if (rt6_insert_exception(nrt6, &res))
  2496. dst_release_immediate(&nrt6->dst);
  2497. }
  2498. out_unlock:
  2499. rcu_read_unlock();
  2500. }
  2501. }
  2502. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  2503. struct sk_buff *skb, u32 mtu,
  2504. bool confirm_neigh)
  2505. {
  2506. __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
  2507. confirm_neigh);
  2508. }
  2509. void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
  2510. int oif, u32 mark, kuid_t uid)
  2511. {
  2512. const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
  2513. struct dst_entry *dst;
  2514. struct flowi6 fl6 = {
  2515. .flowi6_oif = oif,
  2516. .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
  2517. .daddr = iph->daddr,
  2518. .saddr = iph->saddr,
  2519. .flowlabel = ip6_flowinfo(iph),
  2520. .flowi6_uid = uid,
  2521. };
  2522. dst = ip6_route_output(net, NULL, &fl6);
  2523. if (!dst->error)
  2524. __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
  2525. dst_release(dst);
  2526. }
  2527. EXPORT_SYMBOL_GPL(ip6_update_pmtu);
  2528. void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
  2529. {
  2530. int oif = sk->sk_bound_dev_if;
  2531. struct dst_entry *dst;
  2532. if (!oif && skb->dev)
  2533. oif = l3mdev_master_ifindex(skb->dev);
  2534. ip6_update_pmtu(skb, sock_net(sk), mtu, oif, READ_ONCE(sk->sk_mark),
  2535. sk->sk_uid);
  2536. dst = __sk_dst_get(sk);
  2537. if (!dst || !dst->obsolete ||
  2538. dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
  2539. return;
  2540. bh_lock_sock(sk);
  2541. if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  2542. ip6_datagram_dst_update(sk, false);
  2543. bh_unlock_sock(sk);
  2544. }
  2545. EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
  2546. void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
  2547. const struct flowi6 *fl6)
  2548. {
  2549. #ifdef CONFIG_IPV6_SUBTREES
  2550. struct ipv6_pinfo *np = inet6_sk(sk);
  2551. #endif
  2552. ip6_dst_store(sk, dst,
  2553. ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
  2554. &sk->sk_v6_daddr : NULL,
  2555. #ifdef CONFIG_IPV6_SUBTREES
  2556. ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
  2557. &np->saddr :
  2558. #endif
  2559. NULL);
  2560. }
  2561. static bool ip6_redirect_nh_match(const struct fib6_result *res,
  2562. struct flowi6 *fl6,
  2563. const struct in6_addr *gw,
  2564. struct rt6_info **ret)
  2565. {
  2566. const struct fib6_nh *nh = res->nh;
  2567. if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
  2568. fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
  2569. return false;
  2570. /* rt_cache's gateway might be different from its 'parent'
  2571. * in the case of an ip redirect.
  2572. * So we keep searching in the exception table if the gateway
  2573. * is different.
  2574. */
  2575. if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
  2576. struct rt6_info *rt_cache;
  2577. rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
  2578. if (rt_cache &&
  2579. ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
  2580. *ret = rt_cache;
  2581. return true;
  2582. }
  2583. return false;
  2584. }
  2585. return true;
  2586. }
  2587. struct fib6_nh_rd_arg {
  2588. struct fib6_result *res;
  2589. struct flowi6 *fl6;
  2590. const struct in6_addr *gw;
  2591. struct rt6_info **ret;
  2592. };
  2593. static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
  2594. {
  2595. struct fib6_nh_rd_arg *arg = _arg;
  2596. arg->res->nh = nh;
  2597. return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
  2598. }
  2599. /* Handle redirects */
  2600. struct ip6rd_flowi {
  2601. struct flowi6 fl6;
  2602. struct in6_addr gateway;
  2603. };
  2604. INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
  2605. struct fib6_table *table,
  2606. struct flowi6 *fl6,
  2607. const struct sk_buff *skb,
  2608. int flags)
  2609. {
  2610. struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
  2611. struct rt6_info *ret = NULL;
  2612. struct fib6_result res = {};
  2613. struct fib6_nh_rd_arg arg = {
  2614. .res = &res,
  2615. .fl6 = fl6,
  2616. .gw = &rdfl->gateway,
  2617. .ret = &ret
  2618. };
  2619. struct fib6_info *rt;
  2620. struct fib6_node *fn;
  2621. /* Get the "current" route for this destination and
  2622. * check if the redirect has come from appropriate router.
  2623. *
  2624. * RFC 4861 specifies that redirects should only be
  2625. * accepted if they come from the nexthop to the target.
  2626. * Due to the way the routes are chosen, this notion
  2627. * is a bit fuzzy and one might need to check all possible
  2628. * routes.
  2629. */
  2630. rcu_read_lock();
  2631. fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  2632. restart:
  2633. for_each_fib6_node_rt_rcu(fn) {
  2634. res.f6i = rt;
  2635. if (fib6_check_expired(rt))
  2636. continue;
  2637. if (rt->fib6_flags & RTF_REJECT)
  2638. break;
  2639. if (unlikely(rt->nh)) {
  2640. if (nexthop_is_blackhole(rt->nh))
  2641. continue;
  2642. /* on match, res->nh is filled in and potentially ret */
  2643. if (nexthop_for_each_fib6_nh(rt->nh,
  2644. fib6_nh_redirect_match,
  2645. &arg))
  2646. goto out;
  2647. } else {
  2648. res.nh = rt->fib6_nh;
  2649. if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
  2650. &ret))
  2651. goto out;
  2652. }
  2653. }
  2654. if (!rt)
  2655. rt = net->ipv6.fib6_null_entry;
  2656. else if (rt->fib6_flags & RTF_REJECT) {
  2657. ret = net->ipv6.ip6_null_entry;
  2658. goto out;
  2659. }
  2660. if (rt == net->ipv6.fib6_null_entry) {
  2661. fn = fib6_backtrack(fn, &fl6->saddr);
  2662. if (fn)
  2663. goto restart;
  2664. }
  2665. res.f6i = rt;
  2666. res.nh = rt->fib6_nh;
  2667. out:
  2668. if (ret) {
  2669. ip6_hold_safe(net, &ret);
  2670. } else {
  2671. res.fib6_flags = res.f6i->fib6_flags;
  2672. res.fib6_type = res.f6i->fib6_type;
  2673. ret = ip6_create_rt_rcu(&res);
  2674. }
  2675. rcu_read_unlock();
  2676. trace_fib6_table_lookup(net, &res, table, fl6);
  2677. return ret;
  2678. };
  2679. static struct dst_entry *ip6_route_redirect(struct net *net,
  2680. const struct flowi6 *fl6,
  2681. const struct sk_buff *skb,
  2682. const struct in6_addr *gateway)
  2683. {
  2684. int flags = RT6_LOOKUP_F_HAS_SADDR;
  2685. struct ip6rd_flowi rdfl;
  2686. rdfl.fl6 = *fl6;
  2687. rdfl.gateway = *gateway;
  2688. return fib6_rule_lookup(net, &rdfl.fl6, skb,
  2689. flags, __ip6_route_redirect);
  2690. }
  2691. void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
  2692. kuid_t uid)
  2693. {
  2694. const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
  2695. struct dst_entry *dst;
  2696. struct flowi6 fl6 = {
  2697. .flowi6_iif = LOOPBACK_IFINDEX,
  2698. .flowi6_oif = oif,
  2699. .flowi6_mark = mark,
  2700. .daddr = iph->daddr,
  2701. .saddr = iph->saddr,
  2702. .flowlabel = ip6_flowinfo(iph),
  2703. .flowi6_uid = uid,
  2704. };
  2705. dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
  2706. rt6_do_redirect(dst, NULL, skb);
  2707. dst_release(dst);
  2708. }
  2709. EXPORT_SYMBOL_GPL(ip6_redirect);
  2710. void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
  2711. {
  2712. const struct ipv6hdr *iph = ipv6_hdr(skb);
  2713. const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
  2714. struct dst_entry *dst;
  2715. struct flowi6 fl6 = {
  2716. .flowi6_iif = LOOPBACK_IFINDEX,
  2717. .flowi6_oif = oif,
  2718. .daddr = msg->dest,
  2719. .saddr = iph->daddr,
  2720. .flowi6_uid = sock_net_uid(net, NULL),
  2721. };
  2722. dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
  2723. rt6_do_redirect(dst, NULL, skb);
  2724. dst_release(dst);
  2725. }
  2726. void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
  2727. {
  2728. ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if,
  2729. READ_ONCE(sk->sk_mark), sk->sk_uid);
  2730. }
  2731. EXPORT_SYMBOL_GPL(ip6_sk_redirect);
  2732. static unsigned int ip6_default_advmss(const struct dst_entry *dst)
  2733. {
  2734. struct net_device *dev = dst->dev;
  2735. unsigned int mtu = dst_mtu(dst);
  2736. struct net *net;
  2737. mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
  2738. rcu_read_lock();
  2739. net = dev_net_rcu(dev);
  2740. if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
  2741. mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
  2742. rcu_read_unlock();
  2743. /*
  2744. * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
  2745. * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
  2746. * IPV6_MAXPLEN is also valid and means: "any MSS,
  2747. * rely only on pmtu discovery"
  2748. */
  2749. if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
  2750. mtu = IPV6_MAXPLEN;
  2751. return mtu;
  2752. }
  2753. INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
  2754. {
  2755. return ip6_dst_mtu_maybe_forward(dst, false);
  2756. }
  2757. EXPORT_INDIRECT_CALLABLE(ip6_mtu);
  2758. /* MTU selection:
  2759. * 1. mtu on route is locked - use it
  2760. * 2. mtu from nexthop exception
  2761. * 3. mtu from egress device
  2762. *
  2763. * based on ip6_dst_mtu_forward and exception logic of
  2764. * rt6_find_cached_rt; called with rcu_read_lock
  2765. */
  2766. u32 ip6_mtu_from_fib6(const struct fib6_result *res,
  2767. const struct in6_addr *daddr,
  2768. const struct in6_addr *saddr)
  2769. {
  2770. const struct fib6_nh *nh = res->nh;
  2771. struct fib6_info *f6i = res->f6i;
  2772. struct inet6_dev *idev;
  2773. struct rt6_info *rt;
  2774. u32 mtu = 0;
  2775. if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
  2776. mtu = f6i->fib6_pmtu;
  2777. if (mtu)
  2778. goto out;
  2779. }
  2780. rt = rt6_find_cached_rt(res, daddr, saddr);
  2781. if (unlikely(rt)) {
  2782. mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
  2783. } else {
  2784. struct net_device *dev = nh->fib_nh_dev;
  2785. mtu = IPV6_MIN_MTU;
  2786. idev = __in6_dev_get(dev);
  2787. if (idev)
  2788. mtu = max_t(u32, mtu, READ_ONCE(idev->cnf.mtu6));
  2789. }
  2790. mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
  2791. out:
  2792. return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
  2793. }
  2794. struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
  2795. struct flowi6 *fl6)
  2796. {
  2797. struct dst_entry *dst;
  2798. struct rt6_info *rt;
  2799. struct inet6_dev *idev = in6_dev_get(dev);
  2800. struct net *net = dev_net(dev);
  2801. if (unlikely(!idev))
  2802. return ERR_PTR(-ENODEV);
  2803. rt = ip6_dst_alloc(net, dev, 0);
  2804. if (unlikely(!rt)) {
  2805. in6_dev_put(idev);
  2806. dst = ERR_PTR(-ENOMEM);
  2807. goto out;
  2808. }
  2809. rt->dst.input = ip6_input;
  2810. rt->dst.output = ip6_output;
  2811. rt->rt6i_gateway = fl6->daddr;
  2812. rt->rt6i_dst.addr = fl6->daddr;
  2813. rt->rt6i_dst.plen = 128;
  2814. rt->rt6i_idev = idev;
  2815. dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
  2816. /* Add this dst into uncached_list so that rt6_disable_ip() can
  2817. * do proper release of the net_device
  2818. */
  2819. rt6_uncached_list_add(rt);
  2820. dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
  2821. out:
  2822. return dst;
  2823. }
  2824. static void ip6_dst_gc(struct dst_ops *ops)
  2825. {
  2826. struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
  2827. int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
  2828. int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
  2829. int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
  2830. unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
  2831. unsigned int val;
  2832. int entries;
  2833. if (time_after(rt_last_gc + rt_min_interval, jiffies))
  2834. goto out;
  2835. fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
  2836. entries = dst_entries_get_slow(ops);
  2837. if (entries < ops->gc_thresh)
  2838. atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
  2839. out:
  2840. val = atomic_read(&net->ipv6.ip6_rt_gc_expire);
  2841. atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity));
  2842. }
  2843. static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
  2844. const struct in6_addr *gw_addr, u32 tbid,
  2845. int flags, struct fib6_result *res)
  2846. {
  2847. struct flowi6 fl6 = {
  2848. .flowi6_oif = cfg->fc_ifindex,
  2849. .daddr = *gw_addr,
  2850. .saddr = cfg->fc_prefsrc,
  2851. };
  2852. struct fib6_table *table;
  2853. int err;
  2854. table = fib6_get_table(net, tbid);
  2855. if (!table)
  2856. return -EINVAL;
  2857. if (!ipv6_addr_any(&cfg->fc_prefsrc))
  2858. flags |= RT6_LOOKUP_F_HAS_SADDR;
  2859. flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
  2860. err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
  2861. if (!err && res->f6i != net->ipv6.fib6_null_entry)
  2862. fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
  2863. cfg->fc_ifindex != 0, NULL, flags);
  2864. return err;
  2865. }
  2866. static int ip6_route_check_nh_onlink(struct net *net,
  2867. struct fib6_config *cfg,
  2868. const struct net_device *dev,
  2869. struct netlink_ext_ack *extack)
  2870. {
  2871. u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
  2872. const struct in6_addr *gw_addr = &cfg->fc_gateway;
  2873. struct fib6_result res = {};
  2874. int err;
  2875. err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
  2876. if (!err && !(res.fib6_flags & RTF_REJECT) &&
  2877. /* ignore match if it is the default route */
  2878. !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
  2879. (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
  2880. NL_SET_ERR_MSG(extack,
  2881. "Nexthop has invalid gateway or device mismatch");
  2882. err = -EINVAL;
  2883. }
  2884. return err;
  2885. }
  2886. static int ip6_route_check_nh(struct net *net,
  2887. struct fib6_config *cfg,
  2888. struct net_device **_dev,
  2889. netdevice_tracker *dev_tracker,
  2890. struct inet6_dev **idev)
  2891. {
  2892. const struct in6_addr *gw_addr = &cfg->fc_gateway;
  2893. struct net_device *dev = _dev ? *_dev : NULL;
  2894. int flags = RT6_LOOKUP_F_IFACE;
  2895. struct fib6_result res = {};
  2896. int err = -EHOSTUNREACH;
  2897. if (cfg->fc_table) {
  2898. err = ip6_nh_lookup_table(net, cfg, gw_addr,
  2899. cfg->fc_table, flags, &res);
  2900. /* gw_addr can not require a gateway or resolve to a reject
  2901. * route. If a device is given, it must match the result.
  2902. */
  2903. if (err || res.fib6_flags & RTF_REJECT ||
  2904. res.nh->fib_nh_gw_family ||
  2905. (dev && dev != res.nh->fib_nh_dev))
  2906. err = -EHOSTUNREACH;
  2907. }
  2908. if (err < 0) {
  2909. struct flowi6 fl6 = {
  2910. .flowi6_oif = cfg->fc_ifindex,
  2911. .daddr = *gw_addr,
  2912. };
  2913. err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
  2914. if (err || res.fib6_flags & RTF_REJECT ||
  2915. res.nh->fib_nh_gw_family)
  2916. err = -EHOSTUNREACH;
  2917. if (err)
  2918. return err;
  2919. fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
  2920. cfg->fc_ifindex != 0, NULL, flags);
  2921. }
  2922. err = 0;
  2923. if (dev) {
  2924. if (dev != res.nh->fib_nh_dev)
  2925. err = -EHOSTUNREACH;
  2926. } else {
  2927. *_dev = dev = res.nh->fib_nh_dev;
  2928. netdev_hold(dev, dev_tracker, GFP_ATOMIC);
  2929. *idev = in6_dev_get(dev);
  2930. }
  2931. return err;
  2932. }
  2933. static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
  2934. struct net_device **_dev,
  2935. netdevice_tracker *dev_tracker,
  2936. struct inet6_dev **idev,
  2937. struct netlink_ext_ack *extack)
  2938. {
  2939. const struct in6_addr *gw_addr = &cfg->fc_gateway;
  2940. int gwa_type = ipv6_addr_type(gw_addr);
  2941. bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
  2942. const struct net_device *dev = *_dev;
  2943. bool need_addr_check = !dev;
  2944. int err = -EINVAL;
  2945. /* if gw_addr is local we will fail to detect this in case
  2946. * address is still TENTATIVE (DAD in progress). rt6_lookup()
  2947. * will return already-added prefix route via interface that
  2948. * prefix route was assigned to, which might be non-loopback.
  2949. */
  2950. if (dev &&
  2951. ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
  2952. NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
  2953. goto out;
  2954. }
  2955. if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
  2956. /* IPv6 strictly inhibits using not link-local
  2957. * addresses as nexthop address.
  2958. * Otherwise, router will not able to send redirects.
  2959. * It is very good, but in some (rare!) circumstances
  2960. * (SIT, PtP, NBMA NOARP links) it is handy to allow
  2961. * some exceptions. --ANK
  2962. * We allow IPv4-mapped nexthops to support RFC4798-type
  2963. * addressing
  2964. */
  2965. if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
  2966. NL_SET_ERR_MSG(extack, "Invalid gateway address");
  2967. goto out;
  2968. }
  2969. rcu_read_lock();
  2970. if (cfg->fc_flags & RTNH_F_ONLINK)
  2971. err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
  2972. else
  2973. err = ip6_route_check_nh(net, cfg, _dev, dev_tracker,
  2974. idev);
  2975. rcu_read_unlock();
  2976. if (err)
  2977. goto out;
  2978. }
  2979. /* reload in case device was changed */
  2980. dev = *_dev;
  2981. err = -EINVAL;
  2982. if (!dev) {
  2983. NL_SET_ERR_MSG(extack, "Egress device not specified");
  2984. goto out;
  2985. } else if (dev->flags & IFF_LOOPBACK) {
  2986. NL_SET_ERR_MSG(extack,
  2987. "Egress device can not be loopback device for this route");
  2988. goto out;
  2989. }
  2990. /* if we did not check gw_addr above, do so now that the
  2991. * egress device has been resolved.
  2992. */
  2993. if (need_addr_check &&
  2994. ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
  2995. NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
  2996. goto out;
  2997. }
  2998. err = 0;
  2999. out:
  3000. return err;
  3001. }
  3002. static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
  3003. {
  3004. if ((flags & RTF_REJECT) ||
  3005. (dev && (dev->flags & IFF_LOOPBACK) &&
  3006. !(addr_type & IPV6_ADDR_LOOPBACK) &&
  3007. !(flags & (RTF_ANYCAST | RTF_LOCAL))))
  3008. return true;
  3009. return false;
  3010. }
  3011. int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
  3012. struct fib6_config *cfg, gfp_t gfp_flags,
  3013. struct netlink_ext_ack *extack)
  3014. {
  3015. netdevice_tracker *dev_tracker = &fib6_nh->fib_nh_dev_tracker;
  3016. struct net_device *dev = NULL;
  3017. struct inet6_dev *idev = NULL;
  3018. int addr_type;
  3019. int err;
  3020. fib6_nh->fib_nh_family = AF_INET6;
  3021. #ifdef CONFIG_IPV6_ROUTER_PREF
  3022. fib6_nh->last_probe = jiffies;
  3023. #endif
  3024. if (cfg->fc_is_fdb) {
  3025. fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
  3026. fib6_nh->fib_nh_gw_family = AF_INET6;
  3027. return 0;
  3028. }
  3029. err = -ENODEV;
  3030. if (cfg->fc_ifindex) {
  3031. dev = netdev_get_by_index(net, cfg->fc_ifindex,
  3032. dev_tracker, gfp_flags);
  3033. if (!dev)
  3034. goto out;
  3035. idev = in6_dev_get(dev);
  3036. if (!idev)
  3037. goto out;
  3038. }
  3039. if (cfg->fc_flags & RTNH_F_ONLINK) {
  3040. if (!dev) {
  3041. NL_SET_ERR_MSG(extack,
  3042. "Nexthop device required for onlink");
  3043. goto out;
  3044. }
  3045. if (!(dev->flags & IFF_UP)) {
  3046. NL_SET_ERR_MSG(extack, "Nexthop device is not up");
  3047. err = -ENETDOWN;
  3048. goto out;
  3049. }
  3050. fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
  3051. }
  3052. fib6_nh->fib_nh_weight = 1;
  3053. /* We cannot add true routes via loopback here,
  3054. * they would result in kernel looping; promote them to reject routes
  3055. */
  3056. addr_type = ipv6_addr_type(&cfg->fc_dst);
  3057. if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
  3058. /* hold loopback dev/idev if we haven't done so. */
  3059. if (dev != net->loopback_dev) {
  3060. if (dev) {
  3061. netdev_put(dev, dev_tracker);
  3062. in6_dev_put(idev);
  3063. }
  3064. dev = net->loopback_dev;
  3065. netdev_hold(dev, dev_tracker, gfp_flags);
  3066. idev = in6_dev_get(dev);
  3067. if (!idev) {
  3068. err = -ENODEV;
  3069. goto out;
  3070. }
  3071. }
  3072. goto pcpu_alloc;
  3073. }
  3074. if (cfg->fc_flags & RTF_GATEWAY) {
  3075. err = ip6_validate_gw(net, cfg, &dev, dev_tracker,
  3076. &idev, extack);
  3077. if (err)
  3078. goto out;
  3079. fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
  3080. fib6_nh->fib_nh_gw_family = AF_INET6;
  3081. }
  3082. err = -ENODEV;
  3083. if (!dev)
  3084. goto out;
  3085. if (!idev || idev->cnf.disable_ipv6) {
  3086. NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
  3087. err = -EACCES;
  3088. goto out;
  3089. }
  3090. if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
  3091. NL_SET_ERR_MSG(extack, "Nexthop device is not up");
  3092. err = -ENETDOWN;
  3093. goto out;
  3094. }
  3095. if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
  3096. !netif_carrier_ok(dev))
  3097. fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  3098. err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
  3099. cfg->fc_encap_type, cfg, gfp_flags, extack);
  3100. if (err)
  3101. goto out;
  3102. pcpu_alloc:
  3103. fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
  3104. if (!fib6_nh->rt6i_pcpu) {
  3105. err = -ENOMEM;
  3106. goto out;
  3107. }
  3108. fib6_nh->fib_nh_dev = dev;
  3109. fib6_nh->fib_nh_oif = dev->ifindex;
  3110. err = 0;
  3111. out:
  3112. if (idev)
  3113. in6_dev_put(idev);
  3114. if (err) {
  3115. fib_nh_common_release(&fib6_nh->nh_common);
  3116. fib6_nh->nh_common.nhc_pcpu_rth_output = NULL;
  3117. fib6_nh->fib_nh_lws = NULL;
  3118. netdev_put(dev, dev_tracker);
  3119. }
  3120. return err;
  3121. }
  3122. void fib6_nh_release(struct fib6_nh *fib6_nh)
  3123. {
  3124. struct rt6_exception_bucket *bucket;
  3125. rcu_read_lock();
  3126. fib6_nh_flush_exceptions(fib6_nh, NULL);
  3127. bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
  3128. if (bucket) {
  3129. rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
  3130. kfree(bucket);
  3131. }
  3132. rcu_read_unlock();
  3133. fib6_nh_release_dsts(fib6_nh);
  3134. free_percpu(fib6_nh->rt6i_pcpu);
  3135. fib_nh_common_release(&fib6_nh->nh_common);
  3136. }
  3137. void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
  3138. {
  3139. int cpu;
  3140. if (!fib6_nh->rt6i_pcpu)
  3141. return;
  3142. for_each_possible_cpu(cpu) {
  3143. struct rt6_info *pcpu_rt, **ppcpu_rt;
  3144. ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
  3145. pcpu_rt = xchg(ppcpu_rt, NULL);
  3146. if (pcpu_rt) {
  3147. dst_dev_put(&pcpu_rt->dst);
  3148. dst_release(&pcpu_rt->dst);
  3149. }
  3150. }
  3151. }
  3152. static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
  3153. gfp_t gfp_flags,
  3154. struct netlink_ext_ack *extack)
  3155. {
  3156. struct net *net = cfg->fc_nlinfo.nl_net;
  3157. struct fib6_info *rt = NULL;
  3158. struct nexthop *nh = NULL;
  3159. struct fib6_table *table;
  3160. struct fib6_nh *fib6_nh;
  3161. int err = -EINVAL;
  3162. int addr_type;
  3163. /* RTF_PCPU is an internal flag; can not be set by userspace */
  3164. if (cfg->fc_flags & RTF_PCPU) {
  3165. NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
  3166. goto out;
  3167. }
  3168. /* RTF_CACHE is an internal flag; can not be set by userspace */
  3169. if (cfg->fc_flags & RTF_CACHE) {
  3170. NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
  3171. goto out;
  3172. }
  3173. if (cfg->fc_type > RTN_MAX) {
  3174. NL_SET_ERR_MSG(extack, "Invalid route type");
  3175. goto out;
  3176. }
  3177. if (cfg->fc_dst_len > 128) {
  3178. NL_SET_ERR_MSG(extack, "Invalid prefix length");
  3179. goto out;
  3180. }
  3181. if (cfg->fc_src_len > 128) {
  3182. NL_SET_ERR_MSG(extack, "Invalid source address length");
  3183. goto out;
  3184. }
  3185. #ifndef CONFIG_IPV6_SUBTREES
  3186. if (cfg->fc_src_len) {
  3187. NL_SET_ERR_MSG(extack,
  3188. "Specifying source address requires IPV6_SUBTREES to be enabled");
  3189. goto out;
  3190. }
  3191. #endif
  3192. if (cfg->fc_nh_id) {
  3193. nh = nexthop_find_by_id(net, cfg->fc_nh_id);
  3194. if (!nh) {
  3195. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  3196. goto out;
  3197. }
  3198. err = fib6_check_nexthop(nh, cfg, extack);
  3199. if (err)
  3200. goto out;
  3201. }
  3202. err = -ENOBUFS;
  3203. if (cfg->fc_nlinfo.nlh &&
  3204. !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
  3205. table = fib6_get_table(net, cfg->fc_table);
  3206. if (!table) {
  3207. pr_warn("NLM_F_CREATE should be specified when creating new route\n");
  3208. table = fib6_new_table(net, cfg->fc_table);
  3209. }
  3210. } else {
  3211. table = fib6_new_table(net, cfg->fc_table);
  3212. }
  3213. if (!table)
  3214. goto out;
  3215. err = -ENOMEM;
  3216. rt = fib6_info_alloc(gfp_flags, !nh);
  3217. if (!rt)
  3218. goto out;
  3219. rt->fib6_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len,
  3220. extack);
  3221. if (IS_ERR(rt->fib6_metrics)) {
  3222. err = PTR_ERR(rt->fib6_metrics);
  3223. /* Do not leave garbage there. */
  3224. rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
  3225. goto out_free;
  3226. }
  3227. if (cfg->fc_flags & RTF_ADDRCONF)
  3228. rt->dst_nocount = true;
  3229. if (cfg->fc_flags & RTF_EXPIRES)
  3230. fib6_set_expires(rt, jiffies +
  3231. clock_t_to_jiffies(cfg->fc_expires));
  3232. if (cfg->fc_protocol == RTPROT_UNSPEC)
  3233. cfg->fc_protocol = RTPROT_BOOT;
  3234. rt->fib6_protocol = cfg->fc_protocol;
  3235. rt->fib6_table = table;
  3236. rt->fib6_metric = cfg->fc_metric;
  3237. rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
  3238. rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
  3239. ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
  3240. rt->fib6_dst.plen = cfg->fc_dst_len;
  3241. #ifdef CONFIG_IPV6_SUBTREES
  3242. ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
  3243. rt->fib6_src.plen = cfg->fc_src_len;
  3244. #endif
  3245. if (nh) {
  3246. if (rt->fib6_src.plen) {
  3247. NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
  3248. err = -EINVAL;
  3249. goto out_free;
  3250. }
  3251. if (!nexthop_get(nh)) {
  3252. NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
  3253. err = -ENOENT;
  3254. goto out_free;
  3255. }
  3256. rt->nh = nh;
  3257. fib6_nh = nexthop_fib6_nh(rt->nh);
  3258. } else {
  3259. err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
  3260. if (err)
  3261. goto out;
  3262. fib6_nh = rt->fib6_nh;
  3263. /* We cannot add true routes via loopback here, they would
  3264. * result in kernel looping; promote them to reject routes
  3265. */
  3266. addr_type = ipv6_addr_type(&cfg->fc_dst);
  3267. if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
  3268. addr_type))
  3269. rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
  3270. }
  3271. if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
  3272. struct net_device *dev = fib6_nh->fib_nh_dev;
  3273. if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
  3274. NL_SET_ERR_MSG(extack, "Invalid source address");
  3275. err = -EINVAL;
  3276. goto out;
  3277. }
  3278. rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
  3279. rt->fib6_prefsrc.plen = 128;
  3280. } else
  3281. rt->fib6_prefsrc.plen = 0;
  3282. return rt;
  3283. out:
  3284. fib6_info_release(rt);
  3285. return ERR_PTR(err);
  3286. out_free:
  3287. ip_fib_metrics_put(rt->fib6_metrics);
  3288. kfree(rt);
  3289. return ERR_PTR(err);
  3290. }
  3291. int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
  3292. struct netlink_ext_ack *extack)
  3293. {
  3294. struct fib6_info *rt;
  3295. int err;
  3296. rt = ip6_route_info_create(cfg, gfp_flags, extack);
  3297. if (IS_ERR(rt))
  3298. return PTR_ERR(rt);
  3299. err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
  3300. fib6_info_release(rt);
  3301. return err;
  3302. }
  3303. static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
  3304. {
  3305. struct net *net = info->nl_net;
  3306. struct fib6_table *table;
  3307. int err;
  3308. if (rt == net->ipv6.fib6_null_entry) {
  3309. err = -ENOENT;
  3310. goto out;
  3311. }
  3312. table = rt->fib6_table;
  3313. spin_lock_bh(&table->tb6_lock);
  3314. err = fib6_del(rt, info);
  3315. spin_unlock_bh(&table->tb6_lock);
  3316. out:
  3317. fib6_info_release(rt);
  3318. return err;
  3319. }
  3320. int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
  3321. {
  3322. struct nl_info info = {
  3323. .nl_net = net,
  3324. .skip_notify = skip_notify
  3325. };
  3326. return __ip6_del_rt(rt, &info);
  3327. }
  3328. static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
  3329. {
  3330. struct nl_info *info = &cfg->fc_nlinfo;
  3331. struct net *net = info->nl_net;
  3332. struct sk_buff *skb = NULL;
  3333. struct fib6_table *table;
  3334. int err = -ENOENT;
  3335. if (rt == net->ipv6.fib6_null_entry)
  3336. goto out_put;
  3337. table = rt->fib6_table;
  3338. spin_lock_bh(&table->tb6_lock);
  3339. if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
  3340. struct fib6_info *sibling, *next_sibling;
  3341. struct fib6_node *fn;
  3342. /* prefer to send a single notification with all hops */
  3343. skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
  3344. if (skb) {
  3345. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  3346. if (rt6_fill_node(net, skb, rt, NULL,
  3347. NULL, NULL, 0, RTM_DELROUTE,
  3348. info->portid, seq, 0) < 0) {
  3349. kfree_skb(skb);
  3350. skb = NULL;
  3351. } else
  3352. info->skip_notify = 1;
  3353. }
  3354. /* 'rt' points to the first sibling route. If it is not the
  3355. * leaf, then we do not need to send a notification. Otherwise,
  3356. * we need to check if the last sibling has a next route or not
  3357. * and emit a replace or delete notification, respectively.
  3358. */
  3359. info->skip_notify_kernel = 1;
  3360. fn = rcu_dereference_protected(rt->fib6_node,
  3361. lockdep_is_held(&table->tb6_lock));
  3362. if (rcu_access_pointer(fn->leaf) == rt) {
  3363. struct fib6_info *last_sibling, *replace_rt;
  3364. last_sibling = list_last_entry(&rt->fib6_siblings,
  3365. struct fib6_info,
  3366. fib6_siblings);
  3367. replace_rt = rcu_dereference_protected(
  3368. last_sibling->fib6_next,
  3369. lockdep_is_held(&table->tb6_lock));
  3370. if (replace_rt)
  3371. call_fib6_entry_notifiers_replace(net,
  3372. replace_rt);
  3373. else
  3374. call_fib6_multipath_entry_notifiers(net,
  3375. FIB_EVENT_ENTRY_DEL,
  3376. rt, rt->fib6_nsiblings,
  3377. NULL);
  3378. }
  3379. list_for_each_entry_safe(sibling, next_sibling,
  3380. &rt->fib6_siblings,
  3381. fib6_siblings) {
  3382. err = fib6_del(sibling, info);
  3383. if (err)
  3384. goto out_unlock;
  3385. }
  3386. }
  3387. err = fib6_del(rt, info);
  3388. out_unlock:
  3389. spin_unlock_bh(&table->tb6_lock);
  3390. out_put:
  3391. fib6_info_release(rt);
  3392. if (skb) {
  3393. rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
  3394. info->nlh, gfp_any());
  3395. }
  3396. return err;
  3397. }
  3398. static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
  3399. {
  3400. int rc = -ESRCH;
  3401. if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
  3402. goto out;
  3403. if (cfg->fc_flags & RTF_GATEWAY &&
  3404. !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
  3405. goto out;
  3406. rc = rt6_remove_exception_rt(rt);
  3407. out:
  3408. return rc;
  3409. }
  3410. static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
  3411. struct fib6_nh *nh)
  3412. {
  3413. struct fib6_result res = {
  3414. .f6i = rt,
  3415. .nh = nh,
  3416. };
  3417. struct rt6_info *rt_cache;
  3418. rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
  3419. if (rt_cache)
  3420. return __ip6_del_cached_rt(rt_cache, cfg);
  3421. return 0;
  3422. }
  3423. struct fib6_nh_del_cached_rt_arg {
  3424. struct fib6_config *cfg;
  3425. struct fib6_info *f6i;
  3426. };
  3427. static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
  3428. {
  3429. struct fib6_nh_del_cached_rt_arg *arg = _arg;
  3430. int rc;
  3431. rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
  3432. return rc != -ESRCH ? rc : 0;
  3433. }
  3434. static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
  3435. {
  3436. struct fib6_nh_del_cached_rt_arg arg = {
  3437. .cfg = cfg,
  3438. .f6i = f6i
  3439. };
  3440. return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
  3441. }
  3442. static int ip6_route_del(struct fib6_config *cfg,
  3443. struct netlink_ext_ack *extack)
  3444. {
  3445. struct fib6_table *table;
  3446. struct fib6_info *rt;
  3447. struct fib6_node *fn;
  3448. int err = -ESRCH;
  3449. table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
  3450. if (!table) {
  3451. NL_SET_ERR_MSG(extack, "FIB table does not exist");
  3452. return err;
  3453. }
  3454. rcu_read_lock();
  3455. fn = fib6_locate(&table->tb6_root,
  3456. &cfg->fc_dst, cfg->fc_dst_len,
  3457. &cfg->fc_src, cfg->fc_src_len,
  3458. !(cfg->fc_flags & RTF_CACHE));
  3459. if (fn) {
  3460. for_each_fib6_node_rt_rcu(fn) {
  3461. struct fib6_nh *nh;
  3462. if (rt->nh && cfg->fc_nh_id &&
  3463. rt->nh->id != cfg->fc_nh_id)
  3464. continue;
  3465. if (cfg->fc_flags & RTF_CACHE) {
  3466. int rc = 0;
  3467. if (rt->nh) {
  3468. rc = ip6_del_cached_rt_nh(cfg, rt);
  3469. } else if (cfg->fc_nh_id) {
  3470. continue;
  3471. } else {
  3472. nh = rt->fib6_nh;
  3473. rc = ip6_del_cached_rt(cfg, rt, nh);
  3474. }
  3475. if (rc != -ESRCH) {
  3476. rcu_read_unlock();
  3477. return rc;
  3478. }
  3479. continue;
  3480. }
  3481. if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
  3482. continue;
  3483. if (cfg->fc_protocol &&
  3484. cfg->fc_protocol != rt->fib6_protocol)
  3485. continue;
  3486. if (rt->nh) {
  3487. if (!fib6_info_hold_safe(rt))
  3488. continue;
  3489. rcu_read_unlock();
  3490. return __ip6_del_rt(rt, &cfg->fc_nlinfo);
  3491. }
  3492. if (cfg->fc_nh_id)
  3493. continue;
  3494. nh = rt->fib6_nh;
  3495. if (cfg->fc_ifindex &&
  3496. (!nh->fib_nh_dev ||
  3497. nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
  3498. continue;
  3499. if (cfg->fc_flags & RTF_GATEWAY &&
  3500. !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
  3501. continue;
  3502. if (!fib6_info_hold_safe(rt))
  3503. continue;
  3504. rcu_read_unlock();
  3505. /* if gateway was specified only delete the one hop */
  3506. if (cfg->fc_flags & RTF_GATEWAY)
  3507. return __ip6_del_rt(rt, &cfg->fc_nlinfo);
  3508. return __ip6_del_rt_siblings(rt, cfg);
  3509. }
  3510. }
  3511. rcu_read_unlock();
  3512. return err;
  3513. }
  3514. static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
  3515. {
  3516. struct netevent_redirect netevent;
  3517. struct rt6_info *rt, *nrt = NULL;
  3518. struct fib6_result res = {};
  3519. struct ndisc_options ndopts;
  3520. struct inet6_dev *in6_dev;
  3521. struct neighbour *neigh;
  3522. struct rd_msg *msg;
  3523. int optlen, on_link;
  3524. u8 *lladdr;
  3525. optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
  3526. optlen -= sizeof(*msg);
  3527. if (optlen < 0) {
  3528. net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
  3529. return;
  3530. }
  3531. msg = (struct rd_msg *)icmp6_hdr(skb);
  3532. if (ipv6_addr_is_multicast(&msg->dest)) {
  3533. net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
  3534. return;
  3535. }
  3536. on_link = 0;
  3537. if (ipv6_addr_equal(&msg->dest, &msg->target)) {
  3538. on_link = 1;
  3539. } else if (ipv6_addr_type(&msg->target) !=
  3540. (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  3541. net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
  3542. return;
  3543. }
  3544. in6_dev = __in6_dev_get(skb->dev);
  3545. if (!in6_dev)
  3546. return;
  3547. if (READ_ONCE(in6_dev->cnf.forwarding) ||
  3548. !READ_ONCE(in6_dev->cnf.accept_redirects))
  3549. return;
  3550. /* RFC2461 8.1:
  3551. * The IP source address of the Redirect MUST be the same as the current
  3552. * first-hop router for the specified ICMP Destination Address.
  3553. */
  3554. if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
  3555. net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
  3556. return;
  3557. }
  3558. lladdr = NULL;
  3559. if (ndopts.nd_opts_tgt_lladdr) {
  3560. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
  3561. skb->dev);
  3562. if (!lladdr) {
  3563. net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
  3564. return;
  3565. }
  3566. }
  3567. rt = dst_rt6_info(dst);
  3568. if (rt->rt6i_flags & RTF_REJECT) {
  3569. net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
  3570. return;
  3571. }
  3572. /* Redirect received -> path was valid.
  3573. * Look, redirects are sent only in response to data packets,
  3574. * so that this nexthop apparently is reachable. --ANK
  3575. */
  3576. dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
  3577. neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
  3578. if (!neigh)
  3579. return;
  3580. /*
  3581. * We have finally decided to accept it.
  3582. */
  3583. ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
  3584. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  3585. NEIGH_UPDATE_F_OVERRIDE|
  3586. (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  3587. NEIGH_UPDATE_F_ISROUTER)),
  3588. NDISC_REDIRECT, &ndopts);
  3589. rcu_read_lock();
  3590. res.f6i = rcu_dereference(rt->from);
  3591. if (!res.f6i)
  3592. goto out;
  3593. if (res.f6i->nh) {
  3594. struct fib6_nh_match_arg arg = {
  3595. .dev = dst->dev,
  3596. .gw = &rt->rt6i_gateway,
  3597. };
  3598. nexthop_for_each_fib6_nh(res.f6i->nh,
  3599. fib6_nh_find_match, &arg);
  3600. /* fib6_info uses a nexthop that does not have fib6_nh
  3601. * using the dst->dev. Should be impossible
  3602. */
  3603. if (!arg.match)
  3604. goto out;
  3605. res.nh = arg.match;
  3606. } else {
  3607. res.nh = res.f6i->fib6_nh;
  3608. }
  3609. res.fib6_flags = res.f6i->fib6_flags;
  3610. res.fib6_type = res.f6i->fib6_type;
  3611. nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
  3612. if (!nrt)
  3613. goto out;
  3614. nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
  3615. if (on_link)
  3616. nrt->rt6i_flags &= ~RTF_GATEWAY;
  3617. nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
  3618. /* rt6_insert_exception() will take care of duplicated exceptions */
  3619. if (rt6_insert_exception(nrt, &res)) {
  3620. dst_release_immediate(&nrt->dst);
  3621. goto out;
  3622. }
  3623. netevent.old = &rt->dst;
  3624. netevent.new = &nrt->dst;
  3625. netevent.daddr = &msg->dest;
  3626. netevent.neigh = neigh;
  3627. call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
  3628. out:
  3629. rcu_read_unlock();
  3630. neigh_release(neigh);
  3631. }
  3632. #ifdef CONFIG_IPV6_ROUTE_INFO
  3633. static struct fib6_info *rt6_get_route_info(struct net *net,
  3634. const struct in6_addr *prefix, int prefixlen,
  3635. const struct in6_addr *gwaddr,
  3636. struct net_device *dev)
  3637. {
  3638. u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
  3639. int ifindex = dev->ifindex;
  3640. struct fib6_node *fn;
  3641. struct fib6_info *rt = NULL;
  3642. struct fib6_table *table;
  3643. table = fib6_get_table(net, tb_id);
  3644. if (!table)
  3645. return NULL;
  3646. rcu_read_lock();
  3647. fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
  3648. if (!fn)
  3649. goto out;
  3650. for_each_fib6_node_rt_rcu(fn) {
  3651. /* these routes do not use nexthops */
  3652. if (rt->nh)
  3653. continue;
  3654. if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
  3655. continue;
  3656. if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
  3657. !rt->fib6_nh->fib_nh_gw_family)
  3658. continue;
  3659. if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
  3660. continue;
  3661. if (!fib6_info_hold_safe(rt))
  3662. continue;
  3663. break;
  3664. }
  3665. out:
  3666. rcu_read_unlock();
  3667. return rt;
  3668. }
  3669. static struct fib6_info *rt6_add_route_info(struct net *net,
  3670. const struct in6_addr *prefix, int prefixlen,
  3671. const struct in6_addr *gwaddr,
  3672. struct net_device *dev,
  3673. unsigned int pref)
  3674. {
  3675. struct fib6_config cfg = {
  3676. .fc_metric = IP6_RT_PRIO_USER,
  3677. .fc_ifindex = dev->ifindex,
  3678. .fc_dst_len = prefixlen,
  3679. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
  3680. RTF_UP | RTF_PREF(pref),
  3681. .fc_protocol = RTPROT_RA,
  3682. .fc_type = RTN_UNICAST,
  3683. .fc_nlinfo.portid = 0,
  3684. .fc_nlinfo.nlh = NULL,
  3685. .fc_nlinfo.nl_net = net,
  3686. };
  3687. cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
  3688. cfg.fc_dst = *prefix;
  3689. cfg.fc_gateway = *gwaddr;
  3690. /* We should treat it as a default route if prefix length is 0. */
  3691. if (!prefixlen)
  3692. cfg.fc_flags |= RTF_DEFAULT;
  3693. ip6_route_add(&cfg, GFP_ATOMIC, NULL);
  3694. return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
  3695. }
  3696. #endif
  3697. struct fib6_info *rt6_get_dflt_router(struct net *net,
  3698. const struct in6_addr *addr,
  3699. struct net_device *dev)
  3700. {
  3701. u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
  3702. struct fib6_info *rt;
  3703. struct fib6_table *table;
  3704. table = fib6_get_table(net, tb_id);
  3705. if (!table)
  3706. return NULL;
  3707. rcu_read_lock();
  3708. for_each_fib6_node_rt_rcu(&table->tb6_root) {
  3709. struct fib6_nh *nh;
  3710. /* RA routes do not use nexthops */
  3711. if (rt->nh)
  3712. continue;
  3713. nh = rt->fib6_nh;
  3714. if (dev == nh->fib_nh_dev &&
  3715. ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
  3716. ipv6_addr_equal(&nh->fib_nh_gw6, addr))
  3717. break;
  3718. }
  3719. if (rt && !fib6_info_hold_safe(rt))
  3720. rt = NULL;
  3721. rcu_read_unlock();
  3722. return rt;
  3723. }
  3724. struct fib6_info *rt6_add_dflt_router(struct net *net,
  3725. const struct in6_addr *gwaddr,
  3726. struct net_device *dev,
  3727. unsigned int pref,
  3728. u32 defrtr_usr_metric,
  3729. int lifetime)
  3730. {
  3731. struct fib6_config cfg = {
  3732. .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
  3733. .fc_metric = defrtr_usr_metric,
  3734. .fc_ifindex = dev->ifindex,
  3735. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
  3736. RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
  3737. .fc_protocol = RTPROT_RA,
  3738. .fc_type = RTN_UNICAST,
  3739. .fc_nlinfo.portid = 0,
  3740. .fc_nlinfo.nlh = NULL,
  3741. .fc_nlinfo.nl_net = net,
  3742. .fc_expires = jiffies_to_clock_t(lifetime * HZ),
  3743. };
  3744. cfg.fc_gateway = *gwaddr;
  3745. if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
  3746. struct fib6_table *table;
  3747. table = fib6_get_table(dev_net(dev), cfg.fc_table);
  3748. if (table)
  3749. table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
  3750. }
  3751. return rt6_get_dflt_router(net, gwaddr, dev);
  3752. }
  3753. static void __rt6_purge_dflt_routers(struct net *net,
  3754. struct fib6_table *table)
  3755. {
  3756. struct fib6_info *rt;
  3757. restart:
  3758. rcu_read_lock();
  3759. for_each_fib6_node_rt_rcu(&table->tb6_root) {
  3760. struct net_device *dev = fib6_info_nh_dev(rt);
  3761. struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
  3762. if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
  3763. (!idev || idev->cnf.accept_ra != 2) &&
  3764. fib6_info_hold_safe(rt)) {
  3765. rcu_read_unlock();
  3766. ip6_del_rt(net, rt, false);
  3767. goto restart;
  3768. }
  3769. }
  3770. rcu_read_unlock();
  3771. table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
  3772. }
  3773. void rt6_purge_dflt_routers(struct net *net)
  3774. {
  3775. struct fib6_table *table;
  3776. struct hlist_head *head;
  3777. unsigned int h;
  3778. rcu_read_lock();
  3779. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  3780. head = &net->ipv6.fib_table_hash[h];
  3781. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  3782. if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
  3783. __rt6_purge_dflt_routers(net, table);
  3784. }
  3785. }
  3786. rcu_read_unlock();
  3787. }
  3788. static void rtmsg_to_fib6_config(struct net *net,
  3789. struct in6_rtmsg *rtmsg,
  3790. struct fib6_config *cfg)
  3791. {
  3792. *cfg = (struct fib6_config){
  3793. .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
  3794. : RT6_TABLE_MAIN,
  3795. .fc_ifindex = rtmsg->rtmsg_ifindex,
  3796. .fc_metric = rtmsg->rtmsg_metric,
  3797. .fc_expires = rtmsg->rtmsg_info,
  3798. .fc_dst_len = rtmsg->rtmsg_dst_len,
  3799. .fc_src_len = rtmsg->rtmsg_src_len,
  3800. .fc_flags = rtmsg->rtmsg_flags,
  3801. .fc_type = rtmsg->rtmsg_type,
  3802. .fc_nlinfo.nl_net = net,
  3803. .fc_dst = rtmsg->rtmsg_dst,
  3804. .fc_src = rtmsg->rtmsg_src,
  3805. .fc_gateway = rtmsg->rtmsg_gateway,
  3806. };
  3807. }
  3808. int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
  3809. {
  3810. struct fib6_config cfg;
  3811. int err;
  3812. if (cmd != SIOCADDRT && cmd != SIOCDELRT)
  3813. return -EINVAL;
  3814. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  3815. return -EPERM;
  3816. rtmsg_to_fib6_config(net, rtmsg, &cfg);
  3817. rtnl_lock();
  3818. switch (cmd) {
  3819. case SIOCADDRT:
  3820. /* Only do the default setting of fc_metric in route adding */
  3821. if (cfg.fc_metric == 0)
  3822. cfg.fc_metric = IP6_RT_PRIO_USER;
  3823. err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
  3824. break;
  3825. case SIOCDELRT:
  3826. err = ip6_route_del(&cfg, NULL);
  3827. break;
  3828. }
  3829. rtnl_unlock();
  3830. return err;
  3831. }
  3832. /*
  3833. * Drop the packet on the floor
  3834. */
  3835. static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
  3836. {
  3837. struct dst_entry *dst = skb_dst(skb);
  3838. struct net *net = dev_net(dst->dev);
  3839. struct inet6_dev *idev;
  3840. SKB_DR(reason);
  3841. int type;
  3842. if (netif_is_l3_master(skb->dev) ||
  3843. dst->dev == net->loopback_dev)
  3844. idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
  3845. else
  3846. idev = ip6_dst_idev(dst);
  3847. switch (ipstats_mib_noroutes) {
  3848. case IPSTATS_MIB_INNOROUTES:
  3849. type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
  3850. if (type == IPV6_ADDR_ANY) {
  3851. SKB_DR_SET(reason, IP_INADDRERRORS);
  3852. IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  3853. break;
  3854. }
  3855. SKB_DR_SET(reason, IP_INNOROUTES);
  3856. fallthrough;
  3857. case IPSTATS_MIB_OUTNOROUTES:
  3858. SKB_DR_OR(reason, IP_OUTNOROUTES);
  3859. IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
  3860. break;
  3861. }
  3862. /* Start over by dropping the dst for l3mdev case */
  3863. if (netif_is_l3_master(skb->dev))
  3864. skb_dst_drop(skb);
  3865. icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
  3866. kfree_skb_reason(skb, reason);
  3867. return 0;
  3868. }
  3869. static int ip6_pkt_discard(struct sk_buff *skb)
  3870. {
  3871. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
  3872. }
  3873. static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  3874. {
  3875. skb->dev = skb_dst(skb)->dev;
  3876. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
  3877. }
  3878. static int ip6_pkt_prohibit(struct sk_buff *skb)
  3879. {
  3880. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
  3881. }
  3882. static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  3883. {
  3884. skb->dev = skb_dst(skb)->dev;
  3885. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
  3886. }
  3887. /*
  3888. * Allocate a dst for local (unicast / anycast) address.
  3889. */
  3890. struct fib6_info *addrconf_f6i_alloc(struct net *net,
  3891. struct inet6_dev *idev,
  3892. const struct in6_addr *addr,
  3893. bool anycast, gfp_t gfp_flags,
  3894. struct netlink_ext_ack *extack)
  3895. {
  3896. struct fib6_config cfg = {
  3897. .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
  3898. .fc_ifindex = idev->dev->ifindex,
  3899. .fc_flags = RTF_UP | RTF_NONEXTHOP,
  3900. .fc_dst = *addr,
  3901. .fc_dst_len = 128,
  3902. .fc_protocol = RTPROT_KERNEL,
  3903. .fc_nlinfo.nl_net = net,
  3904. .fc_ignore_dev_down = true,
  3905. };
  3906. struct fib6_info *f6i;
  3907. if (anycast) {
  3908. cfg.fc_type = RTN_ANYCAST;
  3909. cfg.fc_flags |= RTF_ANYCAST;
  3910. } else {
  3911. cfg.fc_type = RTN_LOCAL;
  3912. cfg.fc_flags |= RTF_LOCAL;
  3913. }
  3914. f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
  3915. if (!IS_ERR(f6i)) {
  3916. f6i->dst_nocount = true;
  3917. if (!anycast &&
  3918. (READ_ONCE(net->ipv6.devconf_all->disable_policy) ||
  3919. READ_ONCE(idev->cnf.disable_policy)))
  3920. f6i->dst_nopolicy = true;
  3921. }
  3922. return f6i;
  3923. }
  3924. /* remove deleted ip from prefsrc entries */
  3925. struct arg_dev_net_ip {
  3926. struct net *net;
  3927. struct in6_addr *addr;
  3928. };
  3929. static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
  3930. {
  3931. struct net *net = ((struct arg_dev_net_ip *)arg)->net;
  3932. struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
  3933. if (!rt->nh &&
  3934. rt != net->ipv6.fib6_null_entry &&
  3935. ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) &&
  3936. !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) {
  3937. spin_lock_bh(&rt6_exception_lock);
  3938. /* remove prefsrc entry */
  3939. rt->fib6_prefsrc.plen = 0;
  3940. spin_unlock_bh(&rt6_exception_lock);
  3941. }
  3942. return 0;
  3943. }
  3944. void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
  3945. {
  3946. struct net *net = dev_net(ifp->idev->dev);
  3947. struct arg_dev_net_ip adni = {
  3948. .net = net,
  3949. .addr = &ifp->addr,
  3950. };
  3951. fib6_clean_all(net, fib6_remove_prefsrc, &adni);
  3952. }
  3953. #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
  3954. /* Remove routers and update dst entries when gateway turn into host. */
  3955. static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
  3956. {
  3957. struct in6_addr *gateway = (struct in6_addr *)arg;
  3958. struct fib6_nh *nh;
  3959. /* RA routes do not use nexthops */
  3960. if (rt->nh)
  3961. return 0;
  3962. nh = rt->fib6_nh;
  3963. if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
  3964. nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
  3965. return -1;
  3966. /* Further clean up cached routes in exception table.
  3967. * This is needed because cached route may have a different
  3968. * gateway than its 'parent' in the case of an ip redirect.
  3969. */
  3970. fib6_nh_exceptions_clean_tohost(nh, gateway);
  3971. return 0;
  3972. }
  3973. void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
  3974. {
  3975. fib6_clean_all(net, fib6_clean_tohost, gateway);
  3976. }
  3977. struct arg_netdev_event {
  3978. const struct net_device *dev;
  3979. union {
  3980. unsigned char nh_flags;
  3981. unsigned long event;
  3982. };
  3983. };
  3984. static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
  3985. {
  3986. struct fib6_info *iter;
  3987. struct fib6_node *fn;
  3988. fn = rcu_dereference_protected(rt->fib6_node,
  3989. lockdep_is_held(&rt->fib6_table->tb6_lock));
  3990. iter = rcu_dereference_protected(fn->leaf,
  3991. lockdep_is_held(&rt->fib6_table->tb6_lock));
  3992. while (iter) {
  3993. if (iter->fib6_metric == rt->fib6_metric &&
  3994. rt6_qualify_for_ecmp(iter))
  3995. return iter;
  3996. iter = rcu_dereference_protected(iter->fib6_next,
  3997. lockdep_is_held(&rt->fib6_table->tb6_lock));
  3998. }
  3999. return NULL;
  4000. }
  4001. /* only called for fib entries with builtin fib6_nh */
  4002. static bool rt6_is_dead(const struct fib6_info *rt)
  4003. {
  4004. if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
  4005. (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
  4006. ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
  4007. return true;
  4008. return false;
  4009. }
  4010. static int rt6_multipath_total_weight(const struct fib6_info *rt)
  4011. {
  4012. struct fib6_info *iter;
  4013. int total = 0;
  4014. if (!rt6_is_dead(rt))
  4015. total += rt->fib6_nh->fib_nh_weight;
  4016. list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
  4017. if (!rt6_is_dead(iter))
  4018. total += iter->fib6_nh->fib_nh_weight;
  4019. }
  4020. return total;
  4021. }
  4022. static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
  4023. {
  4024. int upper_bound = -1;
  4025. if (!rt6_is_dead(rt)) {
  4026. *weight += rt->fib6_nh->fib_nh_weight;
  4027. upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
  4028. total) - 1;
  4029. }
  4030. atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
  4031. }
  4032. static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
  4033. {
  4034. struct fib6_info *iter;
  4035. int weight = 0;
  4036. rt6_upper_bound_set(rt, &weight, total);
  4037. list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
  4038. rt6_upper_bound_set(iter, &weight, total);
  4039. }
  4040. void rt6_multipath_rebalance(struct fib6_info *rt)
  4041. {
  4042. struct fib6_info *first;
  4043. int total;
  4044. /* In case the entire multipath route was marked for flushing,
  4045. * then there is no need to rebalance upon the removal of every
  4046. * sibling route.
  4047. */
  4048. if (!rt->fib6_nsiblings || rt->should_flush)
  4049. return;
  4050. /* During lookup routes are evaluated in order, so we need to
  4051. * make sure upper bounds are assigned from the first sibling
  4052. * onwards.
  4053. */
  4054. first = rt6_multipath_first_sibling(rt);
  4055. if (WARN_ON_ONCE(!first))
  4056. return;
  4057. total = rt6_multipath_total_weight(first);
  4058. rt6_multipath_upper_bound_set(first, total);
  4059. }
  4060. static int fib6_ifup(struct fib6_info *rt, void *p_arg)
  4061. {
  4062. const struct arg_netdev_event *arg = p_arg;
  4063. struct net *net = dev_net(arg->dev);
  4064. if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
  4065. rt->fib6_nh->fib_nh_dev == arg->dev) {
  4066. rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
  4067. fib6_update_sernum_upto_root(net, rt);
  4068. rt6_multipath_rebalance(rt);
  4069. }
  4070. return 0;
  4071. }
  4072. void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
  4073. {
  4074. struct arg_netdev_event arg = {
  4075. .dev = dev,
  4076. {
  4077. .nh_flags = nh_flags,
  4078. },
  4079. };
  4080. if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
  4081. arg.nh_flags |= RTNH_F_LINKDOWN;
  4082. fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
  4083. }
  4084. /* only called for fib entries with inline fib6_nh */
  4085. static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
  4086. const struct net_device *dev)
  4087. {
  4088. struct fib6_info *iter;
  4089. if (rt->fib6_nh->fib_nh_dev == dev)
  4090. return true;
  4091. list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
  4092. if (iter->fib6_nh->fib_nh_dev == dev)
  4093. return true;
  4094. return false;
  4095. }
  4096. static void rt6_multipath_flush(struct fib6_info *rt)
  4097. {
  4098. struct fib6_info *iter;
  4099. rt->should_flush = 1;
  4100. list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
  4101. iter->should_flush = 1;
  4102. }
  4103. static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
  4104. const struct net_device *down_dev)
  4105. {
  4106. struct fib6_info *iter;
  4107. unsigned int dead = 0;
  4108. if (rt->fib6_nh->fib_nh_dev == down_dev ||
  4109. rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
  4110. dead++;
  4111. list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
  4112. if (iter->fib6_nh->fib_nh_dev == down_dev ||
  4113. iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
  4114. dead++;
  4115. return dead;
  4116. }
  4117. static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
  4118. const struct net_device *dev,
  4119. unsigned char nh_flags)
  4120. {
  4121. struct fib6_info *iter;
  4122. if (rt->fib6_nh->fib_nh_dev == dev)
  4123. rt->fib6_nh->fib_nh_flags |= nh_flags;
  4124. list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
  4125. if (iter->fib6_nh->fib_nh_dev == dev)
  4126. iter->fib6_nh->fib_nh_flags |= nh_flags;
  4127. }
  4128. /* called with write lock held for table with rt */
  4129. static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
  4130. {
  4131. const struct arg_netdev_event *arg = p_arg;
  4132. const struct net_device *dev = arg->dev;
  4133. struct net *net = dev_net(dev);
  4134. if (rt == net->ipv6.fib6_null_entry || rt->nh)
  4135. return 0;
  4136. switch (arg->event) {
  4137. case NETDEV_UNREGISTER:
  4138. return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
  4139. case NETDEV_DOWN:
  4140. if (rt->should_flush)
  4141. return -1;
  4142. if (!rt->fib6_nsiblings)
  4143. return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
  4144. if (rt6_multipath_uses_dev(rt, dev)) {
  4145. unsigned int count;
  4146. count = rt6_multipath_dead_count(rt, dev);
  4147. if (rt->fib6_nsiblings + 1 == count) {
  4148. rt6_multipath_flush(rt);
  4149. return -1;
  4150. }
  4151. rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
  4152. RTNH_F_LINKDOWN);
  4153. fib6_update_sernum(net, rt);
  4154. rt6_multipath_rebalance(rt);
  4155. }
  4156. return -2;
  4157. case NETDEV_CHANGE:
  4158. if (rt->fib6_nh->fib_nh_dev != dev ||
  4159. rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
  4160. break;
  4161. rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
  4162. rt6_multipath_rebalance(rt);
  4163. break;
  4164. }
  4165. return 0;
  4166. }
  4167. void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
  4168. {
  4169. struct arg_netdev_event arg = {
  4170. .dev = dev,
  4171. {
  4172. .event = event,
  4173. },
  4174. };
  4175. struct net *net = dev_net(dev);
  4176. if (net->ipv6.sysctl.skip_notify_on_dev_down)
  4177. fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
  4178. else
  4179. fib6_clean_all(net, fib6_ifdown, &arg);
  4180. }
  4181. void rt6_disable_ip(struct net_device *dev, unsigned long event)
  4182. {
  4183. rt6_sync_down_dev(dev, event);
  4184. rt6_uncached_list_flush_dev(dev);
  4185. neigh_ifdown(&nd_tbl, dev);
  4186. }
  4187. struct rt6_mtu_change_arg {
  4188. struct net_device *dev;
  4189. unsigned int mtu;
  4190. struct fib6_info *f6i;
  4191. };
  4192. static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
  4193. {
  4194. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
  4195. struct fib6_info *f6i = arg->f6i;
  4196. /* For administrative MTU increase, there is no way to discover
  4197. * IPv6 PMTU increase, so PMTU increase should be updated here.
  4198. * Since RFC 1981 doesn't include administrative MTU increase
  4199. * update PMTU increase is a MUST. (i.e. jumbo frame)
  4200. */
  4201. if (nh->fib_nh_dev == arg->dev) {
  4202. struct inet6_dev *idev = __in6_dev_get(arg->dev);
  4203. u32 mtu = f6i->fib6_pmtu;
  4204. if (mtu >= arg->mtu ||
  4205. (mtu < arg->mtu && mtu == idev->cnf.mtu6))
  4206. fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
  4207. spin_lock_bh(&rt6_exception_lock);
  4208. rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
  4209. spin_unlock_bh(&rt6_exception_lock);
  4210. }
  4211. return 0;
  4212. }
  4213. static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
  4214. {
  4215. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
  4216. struct inet6_dev *idev;
  4217. /* In IPv6 pmtu discovery is not optional,
  4218. so that RTAX_MTU lock cannot disable it.
  4219. We still use this lock to block changes
  4220. caused by addrconf/ndisc.
  4221. */
  4222. idev = __in6_dev_get(arg->dev);
  4223. if (!idev)
  4224. return 0;
  4225. if (fib6_metric_locked(f6i, RTAX_MTU))
  4226. return 0;
  4227. arg->f6i = f6i;
  4228. if (f6i->nh) {
  4229. /* fib6_nh_mtu_change only returns 0, so this is safe */
  4230. return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
  4231. arg);
  4232. }
  4233. return fib6_nh_mtu_change(f6i->fib6_nh, arg);
  4234. }
  4235. void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
  4236. {
  4237. struct rt6_mtu_change_arg arg = {
  4238. .dev = dev,
  4239. .mtu = mtu,
  4240. };
  4241. fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
  4242. }
  4243. static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
  4244. [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
  4245. [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
  4246. [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
  4247. [RTA_OIF] = { .type = NLA_U32 },
  4248. [RTA_IIF] = { .type = NLA_U32 },
  4249. [RTA_PRIORITY] = { .type = NLA_U32 },
  4250. [RTA_METRICS] = { .type = NLA_NESTED },
  4251. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  4252. [RTA_PREF] = { .type = NLA_U8 },
  4253. [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
  4254. [RTA_ENCAP] = { .type = NLA_NESTED },
  4255. [RTA_EXPIRES] = { .type = NLA_U32 },
  4256. [RTA_UID] = { .type = NLA_U32 },
  4257. [RTA_MARK] = { .type = NLA_U32 },
  4258. [RTA_TABLE] = { .type = NLA_U32 },
  4259. [RTA_IP_PROTO] = { .type = NLA_U8 },
  4260. [RTA_SPORT] = { .type = NLA_U16 },
  4261. [RTA_DPORT] = { .type = NLA_U16 },
  4262. [RTA_NH_ID] = { .type = NLA_U32 },
  4263. };
  4264. static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  4265. struct fib6_config *cfg,
  4266. struct netlink_ext_ack *extack)
  4267. {
  4268. struct rtmsg *rtm;
  4269. struct nlattr *tb[RTA_MAX+1];
  4270. unsigned int pref;
  4271. int err;
  4272. err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  4273. rtm_ipv6_policy, extack);
  4274. if (err < 0)
  4275. goto errout;
  4276. err = -EINVAL;
  4277. rtm = nlmsg_data(nlh);
  4278. if (rtm->rtm_tos) {
  4279. NL_SET_ERR_MSG(extack,
  4280. "Invalid dsfield (tos): option not available for IPv6");
  4281. goto errout;
  4282. }
  4283. *cfg = (struct fib6_config){
  4284. .fc_table = rtm->rtm_table,
  4285. .fc_dst_len = rtm->rtm_dst_len,
  4286. .fc_src_len = rtm->rtm_src_len,
  4287. .fc_flags = RTF_UP,
  4288. .fc_protocol = rtm->rtm_protocol,
  4289. .fc_type = rtm->rtm_type,
  4290. .fc_nlinfo.portid = NETLINK_CB(skb).portid,
  4291. .fc_nlinfo.nlh = nlh,
  4292. .fc_nlinfo.nl_net = sock_net(skb->sk),
  4293. };
  4294. if (rtm->rtm_type == RTN_UNREACHABLE ||
  4295. rtm->rtm_type == RTN_BLACKHOLE ||
  4296. rtm->rtm_type == RTN_PROHIBIT ||
  4297. rtm->rtm_type == RTN_THROW)
  4298. cfg->fc_flags |= RTF_REJECT;
  4299. if (rtm->rtm_type == RTN_LOCAL)
  4300. cfg->fc_flags |= RTF_LOCAL;
  4301. if (rtm->rtm_flags & RTM_F_CLONED)
  4302. cfg->fc_flags |= RTF_CACHE;
  4303. cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
  4304. if (tb[RTA_NH_ID]) {
  4305. if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
  4306. tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
  4307. NL_SET_ERR_MSG(extack,
  4308. "Nexthop specification and nexthop id are mutually exclusive");
  4309. goto errout;
  4310. }
  4311. cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
  4312. }
  4313. if (tb[RTA_GATEWAY]) {
  4314. cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
  4315. cfg->fc_flags |= RTF_GATEWAY;
  4316. }
  4317. if (tb[RTA_VIA]) {
  4318. NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
  4319. goto errout;
  4320. }
  4321. if (tb[RTA_DST]) {
  4322. int plen = (rtm->rtm_dst_len + 7) >> 3;
  4323. if (nla_len(tb[RTA_DST]) < plen)
  4324. goto errout;
  4325. nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
  4326. }
  4327. if (tb[RTA_SRC]) {
  4328. int plen = (rtm->rtm_src_len + 7) >> 3;
  4329. if (nla_len(tb[RTA_SRC]) < plen)
  4330. goto errout;
  4331. nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
  4332. }
  4333. if (tb[RTA_PREFSRC])
  4334. cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
  4335. if (tb[RTA_OIF])
  4336. cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
  4337. if (tb[RTA_PRIORITY])
  4338. cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
  4339. if (tb[RTA_METRICS]) {
  4340. cfg->fc_mx = nla_data(tb[RTA_METRICS]);
  4341. cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
  4342. }
  4343. if (tb[RTA_TABLE])
  4344. cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
  4345. if (tb[RTA_MULTIPATH]) {
  4346. cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
  4347. cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
  4348. err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
  4349. cfg->fc_mp_len, extack);
  4350. if (err < 0)
  4351. goto errout;
  4352. }
  4353. if (tb[RTA_PREF]) {
  4354. pref = nla_get_u8(tb[RTA_PREF]);
  4355. if (pref != ICMPV6_ROUTER_PREF_LOW &&
  4356. pref != ICMPV6_ROUTER_PREF_HIGH)
  4357. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  4358. cfg->fc_flags |= RTF_PREF(pref);
  4359. }
  4360. if (tb[RTA_ENCAP])
  4361. cfg->fc_encap = tb[RTA_ENCAP];
  4362. if (tb[RTA_ENCAP_TYPE]) {
  4363. cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
  4364. err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
  4365. if (err < 0)
  4366. goto errout;
  4367. }
  4368. if (tb[RTA_EXPIRES]) {
  4369. unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
  4370. if (addrconf_finite_timeout(timeout)) {
  4371. cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
  4372. cfg->fc_flags |= RTF_EXPIRES;
  4373. }
  4374. }
  4375. err = 0;
  4376. errout:
  4377. return err;
  4378. }
  4379. struct rt6_nh {
  4380. struct fib6_info *fib6_info;
  4381. struct fib6_config r_cfg;
  4382. struct list_head next;
  4383. };
  4384. static int ip6_route_info_append(struct net *net,
  4385. struct list_head *rt6_nh_list,
  4386. struct fib6_info *rt,
  4387. struct fib6_config *r_cfg)
  4388. {
  4389. struct rt6_nh *nh;
  4390. int err = -EEXIST;
  4391. list_for_each_entry(nh, rt6_nh_list, next) {
  4392. /* check if fib6_info already exists */
  4393. if (rt6_duplicate_nexthop(nh->fib6_info, rt))
  4394. return err;
  4395. }
  4396. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  4397. if (!nh)
  4398. return -ENOMEM;
  4399. nh->fib6_info = rt;
  4400. memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
  4401. list_add_tail(&nh->next, rt6_nh_list);
  4402. return 0;
  4403. }
  4404. static void ip6_route_mpath_notify(struct fib6_info *rt,
  4405. struct fib6_info *rt_last,
  4406. struct nl_info *info,
  4407. __u16 nlflags)
  4408. {
  4409. /* if this is an APPEND route, then rt points to the first route
  4410. * inserted and rt_last points to last route inserted. Userspace
  4411. * wants a consistent dump of the route which starts at the first
  4412. * nexthop. Since sibling routes are always added at the end of
  4413. * the list, find the first sibling of the last route appended
  4414. */
  4415. rcu_read_lock();
  4416. if ((nlflags & NLM_F_APPEND) && rt_last &&
  4417. READ_ONCE(rt_last->fib6_nsiblings)) {
  4418. rt = list_first_or_null_rcu(&rt_last->fib6_siblings,
  4419. struct fib6_info,
  4420. fib6_siblings);
  4421. }
  4422. if (rt)
  4423. inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
  4424. rcu_read_unlock();
  4425. }
  4426. static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
  4427. {
  4428. bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
  4429. bool should_notify = false;
  4430. struct fib6_info *leaf;
  4431. struct fib6_node *fn;
  4432. rcu_read_lock();
  4433. fn = rcu_dereference(rt->fib6_node);
  4434. if (!fn)
  4435. goto out;
  4436. leaf = rcu_dereference(fn->leaf);
  4437. if (!leaf)
  4438. goto out;
  4439. if (rt == leaf ||
  4440. (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
  4441. rt6_qualify_for_ecmp(leaf)))
  4442. should_notify = true;
  4443. out:
  4444. rcu_read_unlock();
  4445. return should_notify;
  4446. }
  4447. static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla,
  4448. struct netlink_ext_ack *extack)
  4449. {
  4450. if (nla_len(nla) < sizeof(*gw)) {
  4451. NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY");
  4452. return -EINVAL;
  4453. }
  4454. *gw = nla_get_in6_addr(nla);
  4455. return 0;
  4456. }
  4457. static int ip6_route_multipath_add(struct fib6_config *cfg,
  4458. struct netlink_ext_ack *extack)
  4459. {
  4460. struct fib6_info *rt_notif = NULL, *rt_last = NULL;
  4461. struct nl_info *info = &cfg->fc_nlinfo;
  4462. struct fib6_config r_cfg;
  4463. struct rtnexthop *rtnh;
  4464. struct fib6_info *rt;
  4465. struct rt6_nh *err_nh;
  4466. struct rt6_nh *nh, *nh_safe;
  4467. __u16 nlflags;
  4468. int remaining;
  4469. int attrlen;
  4470. int err = 1;
  4471. int nhn = 0;
  4472. int replace = (cfg->fc_nlinfo.nlh &&
  4473. (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
  4474. LIST_HEAD(rt6_nh_list);
  4475. nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
  4476. if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
  4477. nlflags |= NLM_F_APPEND;
  4478. remaining = cfg->fc_mp_len;
  4479. rtnh = (struct rtnexthop *)cfg->fc_mp;
  4480. /* Parse a Multipath Entry and build a list (rt6_nh_list) of
  4481. * fib6_info structs per nexthop
  4482. */
  4483. while (rtnh_ok(rtnh, remaining)) {
  4484. memcpy(&r_cfg, cfg, sizeof(*cfg));
  4485. if (rtnh->rtnh_ifindex)
  4486. r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
  4487. attrlen = rtnh_attrlen(rtnh);
  4488. if (attrlen > 0) {
  4489. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  4490. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  4491. if (nla) {
  4492. err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
  4493. extack);
  4494. if (err)
  4495. goto cleanup;
  4496. r_cfg.fc_flags |= RTF_GATEWAY;
  4497. }
  4498. r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
  4499. /* RTA_ENCAP_TYPE length checked in
  4500. * lwtunnel_valid_encap_type_attr
  4501. */
  4502. nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  4503. if (nla)
  4504. r_cfg.fc_encap_type = nla_get_u16(nla);
  4505. }
  4506. r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
  4507. rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
  4508. if (IS_ERR(rt)) {
  4509. err = PTR_ERR(rt);
  4510. rt = NULL;
  4511. goto cleanup;
  4512. }
  4513. if (!rt6_qualify_for_ecmp(rt)) {
  4514. err = -EINVAL;
  4515. NL_SET_ERR_MSG(extack,
  4516. "Device only routes can not be added for IPv6 using the multipath API.");
  4517. fib6_info_release(rt);
  4518. goto cleanup;
  4519. }
  4520. rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
  4521. err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
  4522. rt, &r_cfg);
  4523. if (err) {
  4524. fib6_info_release(rt);
  4525. goto cleanup;
  4526. }
  4527. rtnh = rtnh_next(rtnh, &remaining);
  4528. }
  4529. if (list_empty(&rt6_nh_list)) {
  4530. NL_SET_ERR_MSG(extack,
  4531. "Invalid nexthop configuration - no valid nexthops");
  4532. return -EINVAL;
  4533. }
  4534. /* for add and replace send one notification with all nexthops.
  4535. * Skip the notification in fib6_add_rt2node and send one with
  4536. * the full route when done
  4537. */
  4538. info->skip_notify = 1;
  4539. /* For add and replace, send one notification with all nexthops. For
  4540. * append, send one notification with all appended nexthops.
  4541. */
  4542. info->skip_notify_kernel = 1;
  4543. err_nh = NULL;
  4544. list_for_each_entry(nh, &rt6_nh_list, next) {
  4545. err = __ip6_ins_rt(nh->fib6_info, info, extack);
  4546. if (err) {
  4547. if (replace && nhn)
  4548. NL_SET_ERR_MSG_MOD(extack,
  4549. "multipath route replace failed (check consistency of installed routes)");
  4550. err_nh = nh;
  4551. goto add_errout;
  4552. }
  4553. /* save reference to last route successfully inserted */
  4554. rt_last = nh->fib6_info;
  4555. /* save reference to first route for notification */
  4556. if (!rt_notif)
  4557. rt_notif = nh->fib6_info;
  4558. /* Because each route is added like a single route we remove
  4559. * these flags after the first nexthop: if there is a collision,
  4560. * we have already failed to add the first nexthop:
  4561. * fib6_add_rt2node() has rejected it; when replacing, old
  4562. * nexthops have been replaced by first new, the rest should
  4563. * be added to it.
  4564. */
  4565. if (cfg->fc_nlinfo.nlh) {
  4566. cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
  4567. NLM_F_REPLACE);
  4568. cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
  4569. }
  4570. nhn++;
  4571. }
  4572. /* An in-kernel notification should only be sent in case the new
  4573. * multipath route is added as the first route in the node, or if
  4574. * it was appended to it. We pass 'rt_notif' since it is the first
  4575. * sibling and might allow us to skip some checks in the replace case.
  4576. */
  4577. if (ip6_route_mpath_should_notify(rt_notif)) {
  4578. enum fib_event_type fib_event;
  4579. if (rt_notif->fib6_nsiblings != nhn - 1)
  4580. fib_event = FIB_EVENT_ENTRY_APPEND;
  4581. else
  4582. fib_event = FIB_EVENT_ENTRY_REPLACE;
  4583. err = call_fib6_multipath_entry_notifiers(info->nl_net,
  4584. fib_event, rt_notif,
  4585. nhn - 1, extack);
  4586. if (err) {
  4587. /* Delete all the siblings that were just added */
  4588. err_nh = NULL;
  4589. goto add_errout;
  4590. }
  4591. }
  4592. /* success ... tell user about new route */
  4593. ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
  4594. goto cleanup;
  4595. add_errout:
  4596. /* send notification for routes that were added so that
  4597. * the delete notifications sent by ip6_route_del are
  4598. * coherent
  4599. */
  4600. if (rt_notif)
  4601. ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
  4602. /* Delete routes that were already added */
  4603. list_for_each_entry(nh, &rt6_nh_list, next) {
  4604. if (err_nh == nh)
  4605. break;
  4606. ip6_route_del(&nh->r_cfg, extack);
  4607. }
  4608. cleanup:
  4609. list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
  4610. fib6_info_release(nh->fib6_info);
  4611. list_del(&nh->next);
  4612. kfree(nh);
  4613. }
  4614. return err;
  4615. }
  4616. static int ip6_route_multipath_del(struct fib6_config *cfg,
  4617. struct netlink_ext_ack *extack)
  4618. {
  4619. struct fib6_config r_cfg;
  4620. struct rtnexthop *rtnh;
  4621. int last_err = 0;
  4622. int remaining;
  4623. int attrlen;
  4624. int err;
  4625. remaining = cfg->fc_mp_len;
  4626. rtnh = (struct rtnexthop *)cfg->fc_mp;
  4627. /* Parse a Multipath Entry */
  4628. while (rtnh_ok(rtnh, remaining)) {
  4629. memcpy(&r_cfg, cfg, sizeof(*cfg));
  4630. if (rtnh->rtnh_ifindex)
  4631. r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
  4632. attrlen = rtnh_attrlen(rtnh);
  4633. if (attrlen > 0) {
  4634. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  4635. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  4636. if (nla) {
  4637. err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
  4638. extack);
  4639. if (err) {
  4640. last_err = err;
  4641. goto next_rtnh;
  4642. }
  4643. r_cfg.fc_flags |= RTF_GATEWAY;
  4644. }
  4645. }
  4646. err = ip6_route_del(&r_cfg, extack);
  4647. if (err)
  4648. last_err = err;
  4649. next_rtnh:
  4650. rtnh = rtnh_next(rtnh, &remaining);
  4651. }
  4652. return last_err;
  4653. }
  4654. static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  4655. struct netlink_ext_ack *extack)
  4656. {
  4657. struct fib6_config cfg;
  4658. int err;
  4659. err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
  4660. if (err < 0)
  4661. return err;
  4662. if (cfg.fc_nh_id &&
  4663. !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
  4664. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  4665. return -EINVAL;
  4666. }
  4667. if (cfg.fc_mp)
  4668. return ip6_route_multipath_del(&cfg, extack);
  4669. else {
  4670. cfg.fc_delete_all_nh = 1;
  4671. return ip6_route_del(&cfg, extack);
  4672. }
  4673. }
  4674. static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  4675. struct netlink_ext_ack *extack)
  4676. {
  4677. struct fib6_config cfg;
  4678. int err;
  4679. err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
  4680. if (err < 0)
  4681. return err;
  4682. if (cfg.fc_metric == 0)
  4683. cfg.fc_metric = IP6_RT_PRIO_USER;
  4684. if (cfg.fc_mp)
  4685. return ip6_route_multipath_add(&cfg, extack);
  4686. else
  4687. return ip6_route_add(&cfg, GFP_KERNEL, extack);
  4688. }
  4689. /* add the overhead of this fib6_nh to nexthop_len */
  4690. static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
  4691. {
  4692. int *nexthop_len = arg;
  4693. *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
  4694. + NLA_ALIGN(sizeof(struct rtnexthop))
  4695. + nla_total_size(16); /* RTA_GATEWAY */
  4696. if (nh->fib_nh_lws) {
  4697. /* RTA_ENCAP_TYPE */
  4698. *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
  4699. /* RTA_ENCAP */
  4700. *nexthop_len += nla_total_size(2);
  4701. }
  4702. return 0;
  4703. }
  4704. static size_t rt6_nlmsg_size(struct fib6_info *f6i)
  4705. {
  4706. struct fib6_info *sibling;
  4707. struct fib6_nh *nh;
  4708. int nexthop_len;
  4709. if (f6i->nh) {
  4710. nexthop_len = nla_total_size(4); /* RTA_NH_ID */
  4711. nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
  4712. &nexthop_len);
  4713. goto common;
  4714. }
  4715. rcu_read_lock();
  4716. retry:
  4717. nh = f6i->fib6_nh;
  4718. nexthop_len = 0;
  4719. if (READ_ONCE(f6i->fib6_nsiblings)) {
  4720. rt6_nh_nlmsg_size(nh, &nexthop_len);
  4721. list_for_each_entry_rcu(sibling, &f6i->fib6_siblings,
  4722. fib6_siblings) {
  4723. rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
  4724. if (!READ_ONCE(f6i->fib6_nsiblings))
  4725. goto retry;
  4726. }
  4727. }
  4728. rcu_read_unlock();
  4729. nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
  4730. common:
  4731. return NLMSG_ALIGN(sizeof(struct rtmsg))
  4732. + nla_total_size(16) /* RTA_SRC */
  4733. + nla_total_size(16) /* RTA_DST */
  4734. + nla_total_size(16) /* RTA_GATEWAY */
  4735. + nla_total_size(16) /* RTA_PREFSRC */
  4736. + nla_total_size(4) /* RTA_TABLE */
  4737. + nla_total_size(4) /* RTA_IIF */
  4738. + nla_total_size(4) /* RTA_OIF */
  4739. + nla_total_size(4) /* RTA_PRIORITY */
  4740. + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
  4741. + nla_total_size(sizeof(struct rta_cacheinfo))
  4742. + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
  4743. + nla_total_size(1) /* RTA_PREF */
  4744. + nexthop_len;
  4745. }
  4746. static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
  4747. unsigned char *flags)
  4748. {
  4749. if (nexthop_is_multipath(nh)) {
  4750. struct nlattr *mp;
  4751. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  4752. if (!mp)
  4753. goto nla_put_failure;
  4754. if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
  4755. goto nla_put_failure;
  4756. nla_nest_end(skb, mp);
  4757. } else {
  4758. struct fib6_nh *fib6_nh;
  4759. fib6_nh = nexthop_fib6_nh(nh);
  4760. if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
  4761. flags, false) < 0)
  4762. goto nla_put_failure;
  4763. }
  4764. return 0;
  4765. nla_put_failure:
  4766. return -EMSGSIZE;
  4767. }
  4768. static int rt6_fill_node(struct net *net, struct sk_buff *skb,
  4769. struct fib6_info *rt, struct dst_entry *dst,
  4770. struct in6_addr *dest, struct in6_addr *src,
  4771. int iif, int type, u32 portid, u32 seq,
  4772. unsigned int flags)
  4773. {
  4774. struct rt6_info *rt6 = dst_rt6_info(dst);
  4775. struct rt6key *rt6_dst, *rt6_src;
  4776. u32 *pmetrics, table, rt6_flags;
  4777. unsigned char nh_flags = 0;
  4778. struct nlmsghdr *nlh;
  4779. struct rtmsg *rtm;
  4780. long expires = 0;
  4781. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
  4782. if (!nlh)
  4783. return -EMSGSIZE;
  4784. if (rt6) {
  4785. rt6_dst = &rt6->rt6i_dst;
  4786. rt6_src = &rt6->rt6i_src;
  4787. rt6_flags = rt6->rt6i_flags;
  4788. } else {
  4789. rt6_dst = &rt->fib6_dst;
  4790. rt6_src = &rt->fib6_src;
  4791. rt6_flags = rt->fib6_flags;
  4792. }
  4793. rtm = nlmsg_data(nlh);
  4794. rtm->rtm_family = AF_INET6;
  4795. rtm->rtm_dst_len = rt6_dst->plen;
  4796. rtm->rtm_src_len = rt6_src->plen;
  4797. rtm->rtm_tos = 0;
  4798. if (rt->fib6_table)
  4799. table = rt->fib6_table->tb6_id;
  4800. else
  4801. table = RT6_TABLE_UNSPEC;
  4802. rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
  4803. if (nla_put_u32(skb, RTA_TABLE, table))
  4804. goto nla_put_failure;
  4805. rtm->rtm_type = rt->fib6_type;
  4806. rtm->rtm_flags = 0;
  4807. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  4808. rtm->rtm_protocol = rt->fib6_protocol;
  4809. if (rt6_flags & RTF_CACHE)
  4810. rtm->rtm_flags |= RTM_F_CLONED;
  4811. if (dest) {
  4812. if (nla_put_in6_addr(skb, RTA_DST, dest))
  4813. goto nla_put_failure;
  4814. rtm->rtm_dst_len = 128;
  4815. } else if (rtm->rtm_dst_len)
  4816. if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
  4817. goto nla_put_failure;
  4818. #ifdef CONFIG_IPV6_SUBTREES
  4819. if (src) {
  4820. if (nla_put_in6_addr(skb, RTA_SRC, src))
  4821. goto nla_put_failure;
  4822. rtm->rtm_src_len = 128;
  4823. } else if (rtm->rtm_src_len &&
  4824. nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
  4825. goto nla_put_failure;
  4826. #endif
  4827. if (iif) {
  4828. #ifdef CONFIG_IPV6_MROUTE
  4829. if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
  4830. int err = ip6mr_get_route(net, skb, rtm, portid);
  4831. if (err == 0)
  4832. return 0;
  4833. if (err < 0)
  4834. goto nla_put_failure;
  4835. } else
  4836. #endif
  4837. if (nla_put_u32(skb, RTA_IIF, iif))
  4838. goto nla_put_failure;
  4839. } else if (dest) {
  4840. struct in6_addr saddr_buf;
  4841. if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 &&
  4842. nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
  4843. goto nla_put_failure;
  4844. }
  4845. if (rt->fib6_prefsrc.plen) {
  4846. struct in6_addr saddr_buf;
  4847. saddr_buf = rt->fib6_prefsrc.addr;
  4848. if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
  4849. goto nla_put_failure;
  4850. }
  4851. pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
  4852. if (rtnetlink_put_metrics(skb, pmetrics) < 0)
  4853. goto nla_put_failure;
  4854. if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
  4855. goto nla_put_failure;
  4856. /* For multipath routes, walk the siblings list and add
  4857. * each as a nexthop within RTA_MULTIPATH.
  4858. */
  4859. if (rt6) {
  4860. if (rt6_flags & RTF_GATEWAY &&
  4861. nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
  4862. goto nla_put_failure;
  4863. if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
  4864. goto nla_put_failure;
  4865. if (dst->lwtstate &&
  4866. lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
  4867. goto nla_put_failure;
  4868. } else if (READ_ONCE(rt->fib6_nsiblings)) {
  4869. struct fib6_info *sibling;
  4870. struct nlattr *mp;
  4871. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  4872. if (!mp)
  4873. goto nla_put_failure;
  4874. if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
  4875. rt->fib6_nh->fib_nh_weight, AF_INET6,
  4876. 0) < 0)
  4877. goto nla_put_failure;
  4878. rcu_read_lock();
  4879. list_for_each_entry_rcu(sibling, &rt->fib6_siblings,
  4880. fib6_siblings) {
  4881. if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
  4882. sibling->fib6_nh->fib_nh_weight,
  4883. AF_INET6, 0) < 0) {
  4884. rcu_read_unlock();
  4885. goto nla_put_failure;
  4886. }
  4887. }
  4888. rcu_read_unlock();
  4889. nla_nest_end(skb, mp);
  4890. } else if (rt->nh) {
  4891. if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
  4892. goto nla_put_failure;
  4893. if (nexthop_is_blackhole(rt->nh))
  4894. rtm->rtm_type = RTN_BLACKHOLE;
  4895. if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
  4896. rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
  4897. goto nla_put_failure;
  4898. rtm->rtm_flags |= nh_flags;
  4899. } else {
  4900. if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
  4901. &nh_flags, false) < 0)
  4902. goto nla_put_failure;
  4903. rtm->rtm_flags |= nh_flags;
  4904. }
  4905. if (rt6_flags & RTF_EXPIRES) {
  4906. expires = dst ? dst->expires : rt->expires;
  4907. expires -= jiffies;
  4908. }
  4909. if (!dst) {
  4910. if (READ_ONCE(rt->offload))
  4911. rtm->rtm_flags |= RTM_F_OFFLOAD;
  4912. if (READ_ONCE(rt->trap))
  4913. rtm->rtm_flags |= RTM_F_TRAP;
  4914. if (READ_ONCE(rt->offload_failed))
  4915. rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
  4916. }
  4917. if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
  4918. goto nla_put_failure;
  4919. if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
  4920. goto nla_put_failure;
  4921. nlmsg_end(skb, nlh);
  4922. return 0;
  4923. nla_put_failure:
  4924. nlmsg_cancel(skb, nlh);
  4925. return -EMSGSIZE;
  4926. }
  4927. static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
  4928. {
  4929. const struct net_device *dev = arg;
  4930. if (nh->fib_nh_dev == dev)
  4931. return 1;
  4932. return 0;
  4933. }
  4934. static bool fib6_info_uses_dev(const struct fib6_info *f6i,
  4935. const struct net_device *dev)
  4936. {
  4937. if (f6i->nh) {
  4938. struct net_device *_dev = (struct net_device *)dev;
  4939. return !!nexthop_for_each_fib6_nh(f6i->nh,
  4940. fib6_info_nh_uses_dev,
  4941. _dev);
  4942. }
  4943. if (f6i->fib6_nh->fib_nh_dev == dev)
  4944. return true;
  4945. if (READ_ONCE(f6i->fib6_nsiblings)) {
  4946. const struct fib6_info *sibling;
  4947. rcu_read_lock();
  4948. list_for_each_entry_rcu(sibling, &f6i->fib6_siblings,
  4949. fib6_siblings) {
  4950. if (sibling->fib6_nh->fib_nh_dev == dev) {
  4951. rcu_read_unlock();
  4952. return true;
  4953. }
  4954. if (!READ_ONCE(f6i->fib6_nsiblings))
  4955. break;
  4956. }
  4957. rcu_read_unlock();
  4958. }
  4959. return false;
  4960. }
  4961. struct fib6_nh_exception_dump_walker {
  4962. struct rt6_rtnl_dump_arg *dump;
  4963. struct fib6_info *rt;
  4964. unsigned int flags;
  4965. unsigned int skip;
  4966. unsigned int count;
  4967. };
  4968. static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
  4969. {
  4970. struct fib6_nh_exception_dump_walker *w = arg;
  4971. struct rt6_rtnl_dump_arg *dump = w->dump;
  4972. struct rt6_exception_bucket *bucket;
  4973. struct rt6_exception *rt6_ex;
  4974. int i, err;
  4975. bucket = fib6_nh_get_excptn_bucket(nh, NULL);
  4976. if (!bucket)
  4977. return 0;
  4978. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  4979. hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
  4980. if (w->skip) {
  4981. w->skip--;
  4982. continue;
  4983. }
  4984. /* Expiration of entries doesn't bump sernum, insertion
  4985. * does. Removal is triggered by insertion, so we can
  4986. * rely on the fact that if entries change between two
  4987. * partial dumps, this node is scanned again completely,
  4988. * see rt6_insert_exception() and fib6_dump_table().
  4989. *
  4990. * Count expired entries we go through as handled
  4991. * entries that we'll skip next time, in case of partial
  4992. * node dump. Otherwise, if entries expire meanwhile,
  4993. * we'll skip the wrong amount.
  4994. */
  4995. if (rt6_check_expired(rt6_ex->rt6i)) {
  4996. w->count++;
  4997. continue;
  4998. }
  4999. err = rt6_fill_node(dump->net, dump->skb, w->rt,
  5000. &rt6_ex->rt6i->dst, NULL, NULL, 0,
  5001. RTM_NEWROUTE,
  5002. NETLINK_CB(dump->cb->skb).portid,
  5003. dump->cb->nlh->nlmsg_seq, w->flags);
  5004. if (err)
  5005. return err;
  5006. w->count++;
  5007. }
  5008. bucket++;
  5009. }
  5010. return 0;
  5011. }
  5012. /* Return -1 if done with node, number of handled routes on partial dump */
  5013. int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
  5014. {
  5015. struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
  5016. struct fib_dump_filter *filter = &arg->filter;
  5017. unsigned int flags = NLM_F_MULTI;
  5018. struct net *net = arg->net;
  5019. int count = 0;
  5020. if (rt == net->ipv6.fib6_null_entry)
  5021. return -1;
  5022. if ((filter->flags & RTM_F_PREFIX) &&
  5023. !(rt->fib6_flags & RTF_PREFIX_RT)) {
  5024. /* success since this is not a prefix route */
  5025. return -1;
  5026. }
  5027. if (filter->filter_set &&
  5028. ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
  5029. (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
  5030. (filter->protocol && rt->fib6_protocol != filter->protocol))) {
  5031. return -1;
  5032. }
  5033. if (filter->filter_set ||
  5034. !filter->dump_routes || !filter->dump_exceptions) {
  5035. flags |= NLM_F_DUMP_FILTERED;
  5036. }
  5037. if (filter->dump_routes) {
  5038. if (skip) {
  5039. skip--;
  5040. } else {
  5041. if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
  5042. 0, RTM_NEWROUTE,
  5043. NETLINK_CB(arg->cb->skb).portid,
  5044. arg->cb->nlh->nlmsg_seq, flags)) {
  5045. return 0;
  5046. }
  5047. count++;
  5048. }
  5049. }
  5050. if (filter->dump_exceptions) {
  5051. struct fib6_nh_exception_dump_walker w = { .dump = arg,
  5052. .rt = rt,
  5053. .flags = flags,
  5054. .skip = skip,
  5055. .count = 0 };
  5056. int err;
  5057. rcu_read_lock();
  5058. if (rt->nh) {
  5059. err = nexthop_for_each_fib6_nh(rt->nh,
  5060. rt6_nh_dump_exceptions,
  5061. &w);
  5062. } else {
  5063. err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
  5064. }
  5065. rcu_read_unlock();
  5066. if (err)
  5067. return count + w.count;
  5068. }
  5069. return -1;
  5070. }
  5071. static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
  5072. const struct nlmsghdr *nlh,
  5073. struct nlattr **tb,
  5074. struct netlink_ext_ack *extack)
  5075. {
  5076. struct rtmsg *rtm;
  5077. int i, err;
  5078. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  5079. NL_SET_ERR_MSG_MOD(extack,
  5080. "Invalid header for get route request");
  5081. return -EINVAL;
  5082. }
  5083. if (!netlink_strict_get_check(skb))
  5084. return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  5085. rtm_ipv6_policy, extack);
  5086. rtm = nlmsg_data(nlh);
  5087. if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
  5088. (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
  5089. rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
  5090. rtm->rtm_type) {
  5091. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
  5092. return -EINVAL;
  5093. }
  5094. if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
  5095. NL_SET_ERR_MSG_MOD(extack,
  5096. "Invalid flags for get route request");
  5097. return -EINVAL;
  5098. }
  5099. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  5100. rtm_ipv6_policy, extack);
  5101. if (err)
  5102. return err;
  5103. if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
  5104. (tb[RTA_DST] && !rtm->rtm_dst_len)) {
  5105. NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
  5106. return -EINVAL;
  5107. }
  5108. for (i = 0; i <= RTA_MAX; i++) {
  5109. if (!tb[i])
  5110. continue;
  5111. switch (i) {
  5112. case RTA_SRC:
  5113. case RTA_DST:
  5114. case RTA_IIF:
  5115. case RTA_OIF:
  5116. case RTA_MARK:
  5117. case RTA_UID:
  5118. case RTA_SPORT:
  5119. case RTA_DPORT:
  5120. case RTA_IP_PROTO:
  5121. break;
  5122. default:
  5123. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
  5124. return -EINVAL;
  5125. }
  5126. }
  5127. return 0;
  5128. }
  5129. static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  5130. struct netlink_ext_ack *extack)
  5131. {
  5132. struct net *net = sock_net(in_skb->sk);
  5133. struct nlattr *tb[RTA_MAX+1];
  5134. int err, iif = 0, oif = 0;
  5135. struct fib6_info *from;
  5136. struct dst_entry *dst;
  5137. struct rt6_info *rt;
  5138. struct sk_buff *skb;
  5139. struct rtmsg *rtm;
  5140. struct flowi6 fl6 = {};
  5141. bool fibmatch;
  5142. err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
  5143. if (err < 0)
  5144. goto errout;
  5145. err = -EINVAL;
  5146. rtm = nlmsg_data(nlh);
  5147. fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
  5148. fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
  5149. if (tb[RTA_SRC]) {
  5150. if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
  5151. goto errout;
  5152. fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
  5153. }
  5154. if (tb[RTA_DST]) {
  5155. if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
  5156. goto errout;
  5157. fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
  5158. }
  5159. if (tb[RTA_IIF])
  5160. iif = nla_get_u32(tb[RTA_IIF]);
  5161. if (tb[RTA_OIF])
  5162. oif = nla_get_u32(tb[RTA_OIF]);
  5163. if (tb[RTA_MARK])
  5164. fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
  5165. if (tb[RTA_UID])
  5166. fl6.flowi6_uid = make_kuid(current_user_ns(),
  5167. nla_get_u32(tb[RTA_UID]));
  5168. else
  5169. fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
  5170. if (tb[RTA_SPORT])
  5171. fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
  5172. if (tb[RTA_DPORT])
  5173. fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
  5174. if (tb[RTA_IP_PROTO]) {
  5175. err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
  5176. &fl6.flowi6_proto, AF_INET6,
  5177. extack);
  5178. if (err)
  5179. goto errout;
  5180. }
  5181. if (iif) {
  5182. struct net_device *dev;
  5183. int flags = 0;
  5184. rcu_read_lock();
  5185. dev = dev_get_by_index_rcu(net, iif);
  5186. if (!dev) {
  5187. rcu_read_unlock();
  5188. err = -ENODEV;
  5189. goto errout;
  5190. }
  5191. fl6.flowi6_iif = iif;
  5192. if (!ipv6_addr_any(&fl6.saddr))
  5193. flags |= RT6_LOOKUP_F_HAS_SADDR;
  5194. dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
  5195. rcu_read_unlock();
  5196. } else {
  5197. fl6.flowi6_oif = oif;
  5198. dst = ip6_route_output(net, NULL, &fl6);
  5199. }
  5200. rt = dst_rt6_info(dst);
  5201. if (rt->dst.error) {
  5202. err = rt->dst.error;
  5203. ip6_rt_put(rt);
  5204. goto errout;
  5205. }
  5206. if (rt == net->ipv6.ip6_null_entry) {
  5207. err = rt->dst.error;
  5208. ip6_rt_put(rt);
  5209. goto errout;
  5210. }
  5211. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  5212. if (!skb) {
  5213. ip6_rt_put(rt);
  5214. err = -ENOBUFS;
  5215. goto errout;
  5216. }
  5217. skb_dst_set(skb, &rt->dst);
  5218. rcu_read_lock();
  5219. from = rcu_dereference(rt->from);
  5220. if (from) {
  5221. if (fibmatch)
  5222. err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
  5223. iif, RTM_NEWROUTE,
  5224. NETLINK_CB(in_skb).portid,
  5225. nlh->nlmsg_seq, 0);
  5226. else
  5227. err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
  5228. &fl6.saddr, iif, RTM_NEWROUTE,
  5229. NETLINK_CB(in_skb).portid,
  5230. nlh->nlmsg_seq, 0);
  5231. } else {
  5232. err = -ENETUNREACH;
  5233. }
  5234. rcu_read_unlock();
  5235. if (err < 0) {
  5236. kfree_skb(skb);
  5237. goto errout;
  5238. }
  5239. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  5240. errout:
  5241. return err;
  5242. }
  5243. void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
  5244. unsigned int nlm_flags)
  5245. {
  5246. struct sk_buff *skb;
  5247. struct net *net = info->nl_net;
  5248. u32 seq;
  5249. int err;
  5250. err = -ENOBUFS;
  5251. seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  5252. skb = nlmsg_new(rt6_nlmsg_size(rt), GFP_ATOMIC);
  5253. if (!skb)
  5254. goto errout;
  5255. err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
  5256. event, info->portid, seq, nlm_flags);
  5257. if (err < 0) {
  5258. /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
  5259. WARN_ON(err == -EMSGSIZE);
  5260. kfree_skb(skb);
  5261. goto errout;
  5262. }
  5263. rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
  5264. info->nlh, GFP_ATOMIC);
  5265. return;
  5266. errout:
  5267. rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
  5268. }
  5269. void fib6_rt_update(struct net *net, struct fib6_info *rt,
  5270. struct nl_info *info)
  5271. {
  5272. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  5273. struct sk_buff *skb;
  5274. int err = -ENOBUFS;
  5275. skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
  5276. if (!skb)
  5277. goto errout;
  5278. err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
  5279. RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
  5280. if (err < 0) {
  5281. /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
  5282. WARN_ON(err == -EMSGSIZE);
  5283. kfree_skb(skb);
  5284. goto errout;
  5285. }
  5286. rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
  5287. info->nlh, gfp_any());
  5288. return;
  5289. errout:
  5290. rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
  5291. }
  5292. void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
  5293. bool offload, bool trap, bool offload_failed)
  5294. {
  5295. struct sk_buff *skb;
  5296. int err;
  5297. if (READ_ONCE(f6i->offload) == offload &&
  5298. READ_ONCE(f6i->trap) == trap &&
  5299. READ_ONCE(f6i->offload_failed) == offload_failed)
  5300. return;
  5301. WRITE_ONCE(f6i->offload, offload);
  5302. WRITE_ONCE(f6i->trap, trap);
  5303. /* 2 means send notifications only if offload_failed was changed. */
  5304. if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
  5305. READ_ONCE(f6i->offload_failed) == offload_failed)
  5306. return;
  5307. WRITE_ONCE(f6i->offload_failed, offload_failed);
  5308. if (!rcu_access_pointer(f6i->fib6_node))
  5309. /* The route was removed from the tree, do not send
  5310. * notification.
  5311. */
  5312. return;
  5313. if (!net->ipv6.sysctl.fib_notify_on_flag_change)
  5314. return;
  5315. skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
  5316. if (!skb) {
  5317. err = -ENOBUFS;
  5318. goto errout;
  5319. }
  5320. err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
  5321. 0, 0);
  5322. if (err < 0) {
  5323. /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
  5324. WARN_ON(err == -EMSGSIZE);
  5325. kfree_skb(skb);
  5326. goto errout;
  5327. }
  5328. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
  5329. return;
  5330. errout:
  5331. rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
  5332. }
  5333. EXPORT_SYMBOL(fib6_info_hw_flags_set);
  5334. static int ip6_route_dev_notify(struct notifier_block *this,
  5335. unsigned long event, void *ptr)
  5336. {
  5337. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  5338. struct net *net = dev_net(dev);
  5339. if (!(dev->flags & IFF_LOOPBACK))
  5340. return NOTIFY_OK;
  5341. if (event == NETDEV_REGISTER) {
  5342. net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
  5343. net->ipv6.ip6_null_entry->dst.dev = dev;
  5344. net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
  5345. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  5346. net->ipv6.ip6_prohibit_entry->dst.dev = dev;
  5347. net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
  5348. net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
  5349. net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
  5350. #endif
  5351. } else if (event == NETDEV_UNREGISTER &&
  5352. dev->reg_state != NETREG_UNREGISTERED) {
  5353. /* NETDEV_UNREGISTER could be fired for multiple times by
  5354. * netdev_wait_allrefs(). Make sure we only call this once.
  5355. */
  5356. in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
  5357. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  5358. in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
  5359. in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
  5360. #endif
  5361. }
  5362. return NOTIFY_OK;
  5363. }
  5364. /*
  5365. * /proc
  5366. */
  5367. #ifdef CONFIG_PROC_FS
  5368. static int rt6_stats_seq_show(struct seq_file *seq, void *v)
  5369. {
  5370. struct net *net = (struct net *)seq->private;
  5371. seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
  5372. net->ipv6.rt6_stats->fib_nodes,
  5373. net->ipv6.rt6_stats->fib_route_nodes,
  5374. atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
  5375. net->ipv6.rt6_stats->fib_rt_entries,
  5376. net->ipv6.rt6_stats->fib_rt_cache,
  5377. dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
  5378. net->ipv6.rt6_stats->fib_discarded_routes);
  5379. return 0;
  5380. }
  5381. #endif /* CONFIG_PROC_FS */
  5382. #ifdef CONFIG_SYSCTL
  5383. static int ipv6_sysctl_rtcache_flush(const struct ctl_table *ctl, int write,
  5384. void *buffer, size_t *lenp, loff_t *ppos)
  5385. {
  5386. struct net *net;
  5387. int delay;
  5388. int ret;
  5389. if (!write)
  5390. return -EINVAL;
  5391. ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  5392. if (ret)
  5393. return ret;
  5394. net = (struct net *)ctl->extra1;
  5395. delay = net->ipv6.sysctl.flush_delay;
  5396. fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
  5397. return 0;
  5398. }
  5399. static struct ctl_table ipv6_route_table_template[] = {
  5400. {
  5401. .procname = "max_size",
  5402. .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
  5403. .maxlen = sizeof(int),
  5404. .mode = 0644,
  5405. .proc_handler = proc_dointvec,
  5406. },
  5407. {
  5408. .procname = "gc_thresh",
  5409. .data = &ip6_dst_ops_template.gc_thresh,
  5410. .maxlen = sizeof(int),
  5411. .mode = 0644,
  5412. .proc_handler = proc_dointvec,
  5413. },
  5414. {
  5415. .procname = "flush",
  5416. .data = &init_net.ipv6.sysctl.flush_delay,
  5417. .maxlen = sizeof(int),
  5418. .mode = 0200,
  5419. .proc_handler = ipv6_sysctl_rtcache_flush
  5420. },
  5421. {
  5422. .procname = "gc_min_interval",
  5423. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  5424. .maxlen = sizeof(int),
  5425. .mode = 0644,
  5426. .proc_handler = proc_dointvec_jiffies,
  5427. },
  5428. {
  5429. .procname = "gc_timeout",
  5430. .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
  5431. .maxlen = sizeof(int),
  5432. .mode = 0644,
  5433. .proc_handler = proc_dointvec_jiffies,
  5434. },
  5435. {
  5436. .procname = "gc_interval",
  5437. .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
  5438. .maxlen = sizeof(int),
  5439. .mode = 0644,
  5440. .proc_handler = proc_dointvec_jiffies,
  5441. },
  5442. {
  5443. .procname = "gc_elasticity",
  5444. .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
  5445. .maxlen = sizeof(int),
  5446. .mode = 0644,
  5447. .proc_handler = proc_dointvec,
  5448. },
  5449. {
  5450. .procname = "mtu_expires",
  5451. .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
  5452. .maxlen = sizeof(int),
  5453. .mode = 0644,
  5454. .proc_handler = proc_dointvec_jiffies,
  5455. },
  5456. {
  5457. .procname = "min_adv_mss",
  5458. .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
  5459. .maxlen = sizeof(int),
  5460. .mode = 0644,
  5461. .proc_handler = proc_dointvec,
  5462. },
  5463. {
  5464. .procname = "gc_min_interval_ms",
  5465. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  5466. .maxlen = sizeof(int),
  5467. .mode = 0644,
  5468. .proc_handler = proc_dointvec_ms_jiffies,
  5469. },
  5470. {
  5471. .procname = "skip_notify_on_dev_down",
  5472. .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
  5473. .maxlen = sizeof(u8),
  5474. .mode = 0644,
  5475. .proc_handler = proc_dou8vec_minmax,
  5476. .extra1 = SYSCTL_ZERO,
  5477. .extra2 = SYSCTL_ONE,
  5478. },
  5479. };
  5480. struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
  5481. {
  5482. struct ctl_table *table;
  5483. table = kmemdup(ipv6_route_table_template,
  5484. sizeof(ipv6_route_table_template),
  5485. GFP_KERNEL);
  5486. if (table) {
  5487. table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
  5488. table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
  5489. table[2].data = &net->ipv6.sysctl.flush_delay;
  5490. table[2].extra1 = net;
  5491. table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  5492. table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
  5493. table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
  5494. table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
  5495. table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
  5496. table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
  5497. table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  5498. table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
  5499. }
  5500. return table;
  5501. }
  5502. size_t ipv6_route_sysctl_table_size(struct net *net)
  5503. {
  5504. /* Don't export sysctls to unprivileged users */
  5505. if (net->user_ns != &init_user_ns)
  5506. return 1;
  5507. return ARRAY_SIZE(ipv6_route_table_template);
  5508. }
  5509. #endif
  5510. static int __net_init ip6_route_net_init(struct net *net)
  5511. {
  5512. int ret = -ENOMEM;
  5513. memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
  5514. sizeof(net->ipv6.ip6_dst_ops));
  5515. if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
  5516. goto out_ip6_dst_ops;
  5517. net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
  5518. if (!net->ipv6.fib6_null_entry)
  5519. goto out_ip6_dst_entries;
  5520. memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
  5521. sizeof(*net->ipv6.fib6_null_entry));
  5522. net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
  5523. sizeof(*net->ipv6.ip6_null_entry),
  5524. GFP_KERNEL);
  5525. if (!net->ipv6.ip6_null_entry)
  5526. goto out_fib6_null_entry;
  5527. net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  5528. dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
  5529. ip6_template_metrics, true);
  5530. INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached);
  5531. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  5532. net->ipv6.fib6_has_custom_rules = false;
  5533. net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
  5534. sizeof(*net->ipv6.ip6_prohibit_entry),
  5535. GFP_KERNEL);
  5536. if (!net->ipv6.ip6_prohibit_entry)
  5537. goto out_ip6_null_entry;
  5538. net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  5539. dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
  5540. ip6_template_metrics, true);
  5541. INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
  5542. net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
  5543. sizeof(*net->ipv6.ip6_blk_hole_entry),
  5544. GFP_KERNEL);
  5545. if (!net->ipv6.ip6_blk_hole_entry)
  5546. goto out_ip6_prohibit_entry;
  5547. net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  5548. dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
  5549. ip6_template_metrics, true);
  5550. INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached);
  5551. #ifdef CONFIG_IPV6_SUBTREES
  5552. net->ipv6.fib6_routes_require_src = 0;
  5553. #endif
  5554. #endif
  5555. net->ipv6.sysctl.flush_delay = 0;
  5556. net->ipv6.sysctl.ip6_rt_max_size = INT_MAX;
  5557. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  5558. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  5559. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  5560. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  5561. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  5562. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  5563. net->ipv6.sysctl.skip_notify_on_dev_down = 0;
  5564. atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
  5565. ret = 0;
  5566. out:
  5567. return ret;
  5568. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  5569. out_ip6_prohibit_entry:
  5570. kfree(net->ipv6.ip6_prohibit_entry);
  5571. out_ip6_null_entry:
  5572. kfree(net->ipv6.ip6_null_entry);
  5573. #endif
  5574. out_fib6_null_entry:
  5575. kfree(net->ipv6.fib6_null_entry);
  5576. out_ip6_dst_entries:
  5577. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  5578. out_ip6_dst_ops:
  5579. goto out;
  5580. }
  5581. static void __net_exit ip6_route_net_exit(struct net *net)
  5582. {
  5583. kfree(net->ipv6.fib6_null_entry);
  5584. kfree(net->ipv6.ip6_null_entry);
  5585. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  5586. kfree(net->ipv6.ip6_prohibit_entry);
  5587. kfree(net->ipv6.ip6_blk_hole_entry);
  5588. #endif
  5589. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  5590. }
  5591. static int __net_init ip6_route_net_init_late(struct net *net)
  5592. {
  5593. #ifdef CONFIG_PROC_FS
  5594. if (!proc_create_net("ipv6_route", 0, net->proc_net,
  5595. &ipv6_route_seq_ops,
  5596. sizeof(struct ipv6_route_iter)))
  5597. return -ENOMEM;
  5598. if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
  5599. rt6_stats_seq_show, NULL)) {
  5600. remove_proc_entry("ipv6_route", net->proc_net);
  5601. return -ENOMEM;
  5602. }
  5603. #endif
  5604. return 0;
  5605. }
  5606. static void __net_exit ip6_route_net_exit_late(struct net *net)
  5607. {
  5608. #ifdef CONFIG_PROC_FS
  5609. remove_proc_entry("ipv6_route", net->proc_net);
  5610. remove_proc_entry("rt6_stats", net->proc_net);
  5611. #endif
  5612. }
  5613. static struct pernet_operations ip6_route_net_ops = {
  5614. .init = ip6_route_net_init,
  5615. .exit = ip6_route_net_exit,
  5616. };
  5617. static int __net_init ipv6_inetpeer_init(struct net *net)
  5618. {
  5619. struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
  5620. if (!bp)
  5621. return -ENOMEM;
  5622. inet_peer_base_init(bp);
  5623. net->ipv6.peers = bp;
  5624. return 0;
  5625. }
  5626. static void __net_exit ipv6_inetpeer_exit(struct net *net)
  5627. {
  5628. struct inet_peer_base *bp = net->ipv6.peers;
  5629. net->ipv6.peers = NULL;
  5630. inetpeer_invalidate_tree(bp);
  5631. kfree(bp);
  5632. }
  5633. static struct pernet_operations ipv6_inetpeer_ops = {
  5634. .init = ipv6_inetpeer_init,
  5635. .exit = ipv6_inetpeer_exit,
  5636. };
  5637. static struct pernet_operations ip6_route_net_late_ops = {
  5638. .init = ip6_route_net_init_late,
  5639. .exit = ip6_route_net_exit_late,
  5640. };
  5641. static struct notifier_block ip6_route_dev_notifier = {
  5642. .notifier_call = ip6_route_dev_notify,
  5643. .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
  5644. };
  5645. void __init ip6_route_init_special_entries(void)
  5646. {
  5647. /* Registering of the loopback is done before this portion of code,
  5648. * the loopback reference in rt6_info will not be taken, do it
  5649. * manually for init_net */
  5650. init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
  5651. init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
  5652. init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  5653. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  5654. init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
  5655. init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  5656. init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
  5657. init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  5658. #endif
  5659. }
  5660. #if IS_BUILTIN(CONFIG_IPV6)
  5661. #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
  5662. DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
  5663. BTF_ID_LIST(btf_fib6_info_id)
  5664. BTF_ID(struct, fib6_info)
  5665. static const struct bpf_iter_seq_info ipv6_route_seq_info = {
  5666. .seq_ops = &ipv6_route_seq_ops,
  5667. .init_seq_private = bpf_iter_init_seq_net,
  5668. .fini_seq_private = bpf_iter_fini_seq_net,
  5669. .seq_priv_size = sizeof(struct ipv6_route_iter),
  5670. };
  5671. static struct bpf_iter_reg ipv6_route_reg_info = {
  5672. .target = "ipv6_route",
  5673. .ctx_arg_info_size = 1,
  5674. .ctx_arg_info = {
  5675. { offsetof(struct bpf_iter__ipv6_route, rt),
  5676. PTR_TO_BTF_ID_OR_NULL },
  5677. },
  5678. .seq_info = &ipv6_route_seq_info,
  5679. };
  5680. static int __init bpf_iter_register(void)
  5681. {
  5682. ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
  5683. return bpf_iter_reg_target(&ipv6_route_reg_info);
  5684. }
  5685. static void bpf_iter_unregister(void)
  5686. {
  5687. bpf_iter_unreg_target(&ipv6_route_reg_info);
  5688. }
  5689. #endif
  5690. #endif
  5691. int __init ip6_route_init(void)
  5692. {
  5693. int ret;
  5694. int cpu;
  5695. ret = -ENOMEM;
  5696. ip6_dst_ops_template.kmem_cachep =
  5697. kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
  5698. SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
  5699. if (!ip6_dst_ops_template.kmem_cachep)
  5700. goto out;
  5701. ret = dst_entries_init(&ip6_dst_blackhole_ops);
  5702. if (ret)
  5703. goto out_kmem_cache;
  5704. ret = register_pernet_subsys(&ipv6_inetpeer_ops);
  5705. if (ret)
  5706. goto out_dst_entries;
  5707. ret = register_pernet_subsys(&ip6_route_net_ops);
  5708. if (ret)
  5709. goto out_register_inetpeer;
  5710. ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
  5711. ret = fib6_init();
  5712. if (ret)
  5713. goto out_register_subsys;
  5714. ret = xfrm6_init();
  5715. if (ret)
  5716. goto out_fib6_init;
  5717. ret = fib6_rules_init();
  5718. if (ret)
  5719. goto xfrm6_init;
  5720. ret = register_pernet_subsys(&ip6_route_net_late_ops);
  5721. if (ret)
  5722. goto fib6_rules_init;
  5723. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
  5724. inet6_rtm_newroute, NULL, 0);
  5725. if (ret < 0)
  5726. goto out_register_late_subsys;
  5727. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
  5728. inet6_rtm_delroute, NULL, 0);
  5729. if (ret < 0)
  5730. goto out_register_late_subsys;
  5731. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
  5732. inet6_rtm_getroute, NULL,
  5733. RTNL_FLAG_DOIT_UNLOCKED);
  5734. if (ret < 0)
  5735. goto out_register_late_subsys;
  5736. ret = register_netdevice_notifier(&ip6_route_dev_notifier);
  5737. if (ret)
  5738. goto out_register_late_subsys;
  5739. #if IS_BUILTIN(CONFIG_IPV6)
  5740. #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
  5741. ret = bpf_iter_register();
  5742. if (ret)
  5743. goto out_register_late_subsys;
  5744. #endif
  5745. #endif
  5746. for_each_possible_cpu(cpu) {
  5747. struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
  5748. INIT_LIST_HEAD(&ul->head);
  5749. spin_lock_init(&ul->lock);
  5750. }
  5751. out:
  5752. return ret;
  5753. out_register_late_subsys:
  5754. rtnl_unregister_all(PF_INET6);
  5755. unregister_pernet_subsys(&ip6_route_net_late_ops);
  5756. fib6_rules_init:
  5757. fib6_rules_cleanup();
  5758. xfrm6_init:
  5759. xfrm6_fini();
  5760. out_fib6_init:
  5761. fib6_gc_cleanup();
  5762. out_register_subsys:
  5763. unregister_pernet_subsys(&ip6_route_net_ops);
  5764. out_register_inetpeer:
  5765. unregister_pernet_subsys(&ipv6_inetpeer_ops);
  5766. out_dst_entries:
  5767. dst_entries_destroy(&ip6_dst_blackhole_ops);
  5768. out_kmem_cache:
  5769. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  5770. goto out;
  5771. }
  5772. void ip6_route_cleanup(void)
  5773. {
  5774. #if IS_BUILTIN(CONFIG_IPV6)
  5775. #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
  5776. bpf_iter_unregister();
  5777. #endif
  5778. #endif
  5779. unregister_netdevice_notifier(&ip6_route_dev_notifier);
  5780. unregister_pernet_subsys(&ip6_route_net_late_ops);
  5781. fib6_rules_cleanup();
  5782. xfrm6_fini();
  5783. fib6_gc_cleanup();
  5784. unregister_pernet_subsys(&ipv6_inetpeer_ops);
  5785. unregister_pernet_subsys(&ip6_route_net_ops);
  5786. dst_entries_destroy(&ip6_dst_blackhole_ops);
  5787. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  5788. }