route.c 167 KB

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