bgp_route.c 530 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986
  1. /* BGP routing information
  2. Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "prefix.h"
  18. #include "linklist.h"
  19. #include "memory.h"
  20. #include "command.h"
  21. #include "stream.h"
  22. #include "filter.h"
  23. #include "str.h"
  24. #include "log.h"
  25. #include "routemap.h"
  26. #include "buffer.h"
  27. #include "sockunion.h"
  28. #include "plist.h"
  29. #include "thread.h"
  30. #include "workqueue.h"
  31. #include "bgpd/bgpd.h"
  32. #include "bgpd/bgp_table.h"
  33. #include "bgpd/bgp_route.h"
  34. #include "bgpd/bgp_attr.h"
  35. #include "bgpd/bgp_debug.h"
  36. #include "bgpd/bgp_aspath.h"
  37. #include "bgpd/bgp_regex.h"
  38. #include "bgpd/bgp_community.h"
  39. #include "bgpd/bgp_ecommunity.h"
  40. #include "bgpd/bgp_clist.h"
  41. #include "bgpd/bgp_packet.h"
  42. #include "bgpd/bgp_filter.h"
  43. #include "bgpd/bgp_fsm.h"
  44. #include "bgpd/bgp_mplsvpn.h"
  45. #include "bgpd/bgp_nexthop.h"
  46. #include "bgpd/bgp_damp.h"
  47. #include "bgpd/bgp_advertise.h"
  48. #include "bgpd/bgp_zebra.h"
  49. #include "bgpd/bgp_vty.h"
  50. #include "bgpd/bgp_mpath.h"
  51. #include "bgpd/bgp_nht.h"
  52. /* Extern from bgp_dump.c */
  53. extern const char *bgp_origin_str[];
  54. extern const char *bgp_origin_long_str[];
  55. static struct bgp_node *
  56. bgp_afi_node_get (struct bgp_table *table, afi_t afi, safi_t safi, struct prefix *p,
  57. struct prefix_rd *prd)
  58. {
  59. struct bgp_node *rn;
  60. struct bgp_node *prn = NULL;
  61. assert (table);
  62. if (!table)
  63. return NULL;
  64. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  65. {
  66. prn = bgp_node_get (table, (struct prefix *) prd);
  67. if (prn->info == NULL)
  68. prn->info = bgp_table_init (afi, safi);
  69. else
  70. bgp_unlock_node (prn);
  71. table = prn->info;
  72. }
  73. rn = bgp_node_get (table, p);
  74. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  75. rn->prn = prn;
  76. return rn;
  77. }
  78. /* Allocate bgp_info_extra */
  79. static struct bgp_info_extra *
  80. bgp_info_extra_new (void)
  81. {
  82. struct bgp_info_extra *new;
  83. new = XCALLOC (MTYPE_BGP_ROUTE_EXTRA, sizeof (struct bgp_info_extra));
  84. return new;
  85. }
  86. static void
  87. bgp_info_extra_free (struct bgp_info_extra **extra)
  88. {
  89. if (extra && *extra)
  90. {
  91. if ((*extra)->damp_info)
  92. bgp_damp_info_free ((*extra)->damp_info, 0);
  93. (*extra)->damp_info = NULL;
  94. XFREE (MTYPE_BGP_ROUTE_EXTRA, *extra);
  95. *extra = NULL;
  96. }
  97. }
  98. /* Get bgp_info extra information for the given bgp_info, lazy allocated
  99. * if required.
  100. */
  101. struct bgp_info_extra *
  102. bgp_info_extra_get (struct bgp_info *ri)
  103. {
  104. if (!ri->extra)
  105. ri->extra = bgp_info_extra_new();
  106. return ri->extra;
  107. }
  108. /* Free bgp route information. */
  109. static void
  110. bgp_info_free (struct bgp_info *binfo)
  111. {
  112. if (binfo->attr)
  113. bgp_attr_unintern (&binfo->attr);
  114. bgp_unlink_nexthop(binfo);
  115. bgp_info_extra_free (&binfo->extra);
  116. bgp_info_mpath_free (&binfo->mpath);
  117. peer_unlock (binfo->peer); /* bgp_info peer reference */
  118. XFREE (MTYPE_BGP_ROUTE, binfo);
  119. }
  120. struct bgp_info *
  121. bgp_info_lock (struct bgp_info *binfo)
  122. {
  123. binfo->lock++;
  124. return binfo;
  125. }
  126. struct bgp_info *
  127. bgp_info_unlock (struct bgp_info *binfo)
  128. {
  129. assert (binfo && binfo->lock > 0);
  130. binfo->lock--;
  131. if (binfo->lock == 0)
  132. {
  133. #if 0
  134. zlog_debug ("%s: unlocked and freeing", __func__);
  135. zlog_backtrace (LOG_DEBUG);
  136. #endif
  137. bgp_info_free (binfo);
  138. return NULL;
  139. }
  140. #if 0
  141. if (binfo->lock == 1)
  142. {
  143. zlog_debug ("%s: unlocked to 1", __func__);
  144. zlog_backtrace (LOG_DEBUG);
  145. }
  146. #endif
  147. return binfo;
  148. }
  149. void
  150. bgp_info_add (struct bgp_node *rn, struct bgp_info *ri)
  151. {
  152. struct bgp_info *top;
  153. top = rn->info;
  154. ri->next = rn->info;
  155. ri->prev = NULL;
  156. if (top)
  157. top->prev = ri;
  158. rn->info = ri;
  159. bgp_info_lock (ri);
  160. bgp_lock_node (rn);
  161. peer_lock (ri->peer); /* bgp_info peer reference */
  162. }
  163. /* Do the actual removal of info from RIB, for use by bgp_process
  164. completion callback *only* */
  165. static void
  166. bgp_info_reap (struct bgp_node *rn, struct bgp_info *ri)
  167. {
  168. if (ri->next)
  169. ri->next->prev = ri->prev;
  170. if (ri->prev)
  171. ri->prev->next = ri->next;
  172. else
  173. rn->info = ri->next;
  174. bgp_info_mpath_dequeue (ri);
  175. bgp_info_unlock (ri);
  176. bgp_unlock_node (rn);
  177. }
  178. void
  179. bgp_info_delete (struct bgp_node *rn, struct bgp_info *ri)
  180. {
  181. bgp_info_set_flag (rn, ri, BGP_INFO_REMOVED);
  182. /* set of previous already took care of pcount */
  183. UNSET_FLAG (ri->flags, BGP_INFO_VALID);
  184. }
  185. /* undo the effects of a previous call to bgp_info_delete; typically
  186. called when a route is deleted and then quickly re-added before the
  187. deletion has been processed */
  188. static void
  189. bgp_info_restore (struct bgp_node *rn, struct bgp_info *ri)
  190. {
  191. bgp_info_unset_flag (rn, ri, BGP_INFO_REMOVED);
  192. /* unset of previous already took care of pcount */
  193. SET_FLAG (ri->flags, BGP_INFO_VALID);
  194. }
  195. /* Adjust pcount as required */
  196. static void
  197. bgp_pcount_adjust (struct bgp_node *rn, struct bgp_info *ri)
  198. {
  199. struct bgp_table *table;
  200. assert (rn && bgp_node_table (rn));
  201. assert (ri && ri->peer && ri->peer->bgp);
  202. table = bgp_node_table (rn);
  203. /* Ignore 'pcount' for RS-client tables */
  204. if (table->type != BGP_TABLE_MAIN
  205. || ri->peer == ri->peer->bgp->peer_self)
  206. return;
  207. if (!BGP_INFO_COUNTABLE (ri)
  208. && CHECK_FLAG (ri->flags, BGP_INFO_COUNTED))
  209. {
  210. UNSET_FLAG (ri->flags, BGP_INFO_COUNTED);
  211. /* slight hack, but more robust against errors. */
  212. if (ri->peer->pcount[table->afi][table->safi])
  213. ri->peer->pcount[table->afi][table->safi]--;
  214. else
  215. {
  216. zlog_warn ("%s: Asked to decrement 0 prefix count for peer %s",
  217. __func__, ri->peer->host);
  218. zlog_backtrace (LOG_WARNING);
  219. zlog_warn ("%s: Please report to Quagga bugzilla", __func__);
  220. }
  221. }
  222. else if (BGP_INFO_COUNTABLE (ri)
  223. && !CHECK_FLAG (ri->flags, BGP_INFO_COUNTED))
  224. {
  225. SET_FLAG (ri->flags, BGP_INFO_COUNTED);
  226. ri->peer->pcount[table->afi][table->safi]++;
  227. }
  228. }
  229. /* Set/unset bgp_info flags, adjusting any other state as needed.
  230. * This is here primarily to keep prefix-count in check.
  231. */
  232. void
  233. bgp_info_set_flag (struct bgp_node *rn, struct bgp_info *ri, u_int32_t flag)
  234. {
  235. SET_FLAG (ri->flags, flag);
  236. /* early bath if we know it's not a flag that changes countability state */
  237. if (!CHECK_FLAG (flag, BGP_INFO_VALID|BGP_INFO_HISTORY|BGP_INFO_REMOVED))
  238. return;
  239. bgp_pcount_adjust (rn, ri);
  240. }
  241. void
  242. bgp_info_unset_flag (struct bgp_node *rn, struct bgp_info *ri, u_int32_t flag)
  243. {
  244. UNSET_FLAG (ri->flags, flag);
  245. /* early bath if we know it's not a flag that changes countability state */
  246. if (!CHECK_FLAG (flag, BGP_INFO_VALID|BGP_INFO_HISTORY|BGP_INFO_REMOVED))
  247. return;
  248. bgp_pcount_adjust (rn, ri);
  249. }
  250. /* Get MED value. If MED value is missing and "bgp bestpath
  251. missing-as-worst" is specified, treat it as the worst value. */
  252. static u_int32_t
  253. bgp_med_value (struct attr *attr, struct bgp *bgp)
  254. {
  255. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  256. return attr->med;
  257. else
  258. {
  259. if (bgp_flag_check (bgp, BGP_FLAG_MED_MISSING_AS_WORST))
  260. return BGP_MED_MAX;
  261. else
  262. return 0;
  263. }
  264. }
  265. /* Compare two bgp route entity. Return -1 if new is preferred, 1 if exist
  266. * is preferred, or 0 if they are the same (usually will only occur if
  267. * multipath is enabled */
  268. static int
  269. bgp_info_cmp (struct bgp *bgp, struct bgp_info *new, struct bgp_info *exist,
  270. afi_t afi, safi_t safi)
  271. {
  272. struct attr *newattr, *existattr;
  273. struct attr_extra *newattre, *existattre;
  274. bgp_peer_sort_t new_sort;
  275. bgp_peer_sort_t exist_sort;
  276. u_int32_t new_pref;
  277. u_int32_t exist_pref;
  278. u_int32_t new_med;
  279. u_int32_t exist_med;
  280. u_int32_t new_weight;
  281. u_int32_t exist_weight;
  282. uint32_t newm, existm;
  283. struct in_addr new_id;
  284. struct in_addr exist_id;
  285. int new_cluster;
  286. int exist_cluster;
  287. int internal_as_route;
  288. int confed_as_route;
  289. int ret;
  290. /* 0. Null check. */
  291. if (new == NULL)
  292. return 1;
  293. if (exist == NULL)
  294. return -1;
  295. newattr = new->attr;
  296. existattr = exist->attr;
  297. newattre = newattr->extra;
  298. existattre = existattr->extra;
  299. /* 1. Weight check. */
  300. new_weight = exist_weight = 0;
  301. if (newattre)
  302. new_weight = newattre->weight;
  303. if (existattre)
  304. exist_weight = existattre->weight;
  305. if (new_weight > exist_weight)
  306. return -1;
  307. if (new_weight < exist_weight)
  308. return 1;
  309. /* 2. Local preference check. */
  310. new_pref = exist_pref = bgp->default_local_pref;
  311. if (newattr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
  312. new_pref = newattr->local_pref;
  313. if (existattr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
  314. exist_pref = existattr->local_pref;
  315. if (new_pref > exist_pref)
  316. return -1;
  317. if (new_pref < exist_pref)
  318. return 1;
  319. /* 3. Local route check. We prefer:
  320. * - BGP_ROUTE_STATIC
  321. * - BGP_ROUTE_AGGREGATE
  322. * - BGP_ROUTE_REDISTRIBUTE
  323. */
  324. if (! (new->sub_type == BGP_ROUTE_NORMAL))
  325. return -1;
  326. if (! (exist->sub_type == BGP_ROUTE_NORMAL))
  327. return 1;
  328. /* 4. AS path length check. */
  329. if (! bgp_flag_check (bgp, BGP_FLAG_ASPATH_IGNORE))
  330. {
  331. int exist_hops = aspath_count_hops (existattr->aspath);
  332. int exist_confeds = aspath_count_confeds (existattr->aspath);
  333. if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_CONFED))
  334. {
  335. int aspath_hops;
  336. aspath_hops = aspath_count_hops (newattr->aspath);
  337. aspath_hops += aspath_count_confeds (newattr->aspath);
  338. if ( aspath_hops < (exist_hops + exist_confeds))
  339. return -1;
  340. if ( aspath_hops > (exist_hops + exist_confeds))
  341. return 1;
  342. }
  343. else
  344. {
  345. int newhops = aspath_count_hops (newattr->aspath);
  346. if (newhops < exist_hops)
  347. return -1;
  348. if (newhops > exist_hops)
  349. return 1;
  350. }
  351. }
  352. /* 5. Origin check. */
  353. if (newattr->origin < existattr->origin)
  354. return -1;
  355. if (newattr->origin > existattr->origin)
  356. return 1;
  357. /* 6. MED check. */
  358. internal_as_route = (aspath_count_hops (newattr->aspath) == 0
  359. && aspath_count_hops (existattr->aspath) == 0);
  360. confed_as_route = (aspath_count_confeds (newattr->aspath) > 0
  361. && aspath_count_confeds (existattr->aspath) > 0
  362. && aspath_count_hops (newattr->aspath) == 0
  363. && aspath_count_hops (existattr->aspath) == 0);
  364. if (bgp_flag_check (bgp, BGP_FLAG_ALWAYS_COMPARE_MED)
  365. || (bgp_flag_check (bgp, BGP_FLAG_MED_CONFED)
  366. && confed_as_route)
  367. || aspath_cmp_left (newattr->aspath, existattr->aspath)
  368. || aspath_cmp_left_confed (newattr->aspath, existattr->aspath)
  369. || internal_as_route)
  370. {
  371. new_med = bgp_med_value (new->attr, bgp);
  372. exist_med = bgp_med_value (exist->attr, bgp);
  373. if (new_med < exist_med)
  374. return -1;
  375. if (new_med > exist_med)
  376. return 1;
  377. }
  378. /* 7. Peer type check. */
  379. new_sort = new->peer->sort;
  380. exist_sort = exist->peer->sort;
  381. if (new_sort == BGP_PEER_EBGP
  382. && (exist_sort == BGP_PEER_IBGP || exist_sort == BGP_PEER_CONFED))
  383. return -1;
  384. if (exist_sort == BGP_PEER_EBGP
  385. && (new_sort == BGP_PEER_IBGP || new_sort == BGP_PEER_CONFED))
  386. return 1;
  387. /* 8. IGP metric check. */
  388. newm = existm = 0;
  389. if (new->extra)
  390. newm = new->extra->igpmetric;
  391. if (exist->extra)
  392. existm = exist->extra->igpmetric;
  393. if (newm < existm)
  394. return -1;
  395. if (newm > existm)
  396. return 1;
  397. /* 9. Maximum path check. */
  398. if (bgp_mpath_is_configured (bgp, afi, safi))
  399. {
  400. if (bgp_flag_check(bgp, BGP_FLAG_ASPATH_MULTIPATH_RELAX))
  401. {
  402. /*
  403. * For the two paths, all comparison steps till IGP metric
  404. * have succeeded - including AS_PATH hop count. Since 'bgp
  405. * bestpath as-path multipath-relax' knob is on, we don't need
  406. * an exact match of AS_PATH. Thus, mark the paths are equal.
  407. * That will trigger both these paths to get into the multipath
  408. * array.
  409. */
  410. return 0;
  411. }
  412. else if (new->peer->sort == BGP_PEER_IBGP)
  413. {
  414. if (aspath_cmp (new->attr->aspath, exist->attr->aspath))
  415. return 0;
  416. }
  417. else if (new->peer->as == exist->peer->as)
  418. return 0;
  419. }
  420. /* 10. If both paths are external, prefer the path that was received
  421. first (the oldest one). This step minimizes route-flap, since a
  422. newer path won't displace an older one, even if it was the
  423. preferred route based on the additional decision criteria below. */
  424. if (! bgp_flag_check (bgp, BGP_FLAG_COMPARE_ROUTER_ID)
  425. && new_sort == BGP_PEER_EBGP
  426. && exist_sort == BGP_PEER_EBGP)
  427. {
  428. if (CHECK_FLAG (new->flags, BGP_INFO_SELECTED))
  429. return -1;
  430. if (CHECK_FLAG (exist->flags, BGP_INFO_SELECTED))
  431. return 1;
  432. }
  433. /* 11. Router-ID comparision. */
  434. /* If one of the paths is "stale", the corresponding peer router-id will
  435. * be 0 and would always win over the other path. If originator id is
  436. * used for the comparision, it will decide which path is better.
  437. */
  438. if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
  439. new_id.s_addr = newattre->originator_id.s_addr;
  440. else
  441. new_id.s_addr = new->peer->remote_id.s_addr;
  442. if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
  443. exist_id.s_addr = existattre->originator_id.s_addr;
  444. else
  445. exist_id.s_addr = exist->peer->remote_id.s_addr;
  446. if (ntohl (new_id.s_addr) < ntohl (exist_id.s_addr))
  447. return -1;
  448. if (ntohl (new_id.s_addr) > ntohl (exist_id.s_addr))
  449. return 1;
  450. /* 12. Cluster length comparision. */
  451. new_cluster = exist_cluster = 0;
  452. if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))
  453. new_cluster = newattre->cluster->length;
  454. if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))
  455. exist_cluster = existattre->cluster->length;
  456. if (new_cluster < exist_cluster)
  457. return -1;
  458. if (new_cluster > exist_cluster)
  459. return 1;
  460. /* 13. Neighbor address comparision. */
  461. /* Do this only if neither path is "stale" as stale paths do not have
  462. * valid peer information (as the connection may or may not be up).
  463. */
  464. if (CHECK_FLAG (exist->flags, BGP_INFO_STALE))
  465. return -1;
  466. if (CHECK_FLAG (new->flags, BGP_INFO_STALE))
  467. return 1;
  468. /* locally configured routes to advertise do not have su_remote */
  469. if (new->peer->su_remote == NULL)
  470. return 1;
  471. if (exist->peer->su_remote == NULL)
  472. return -1;
  473. ret = sockunion_cmp (new->peer->su_remote, exist->peer->su_remote);
  474. if (ret == 1)
  475. return 1;
  476. if (ret == -1)
  477. return -1;
  478. return -1;
  479. }
  480. static enum filter_type
  481. bgp_input_filter (struct peer *peer, struct prefix *p, struct attr *attr,
  482. afi_t afi, safi_t safi)
  483. {
  484. struct bgp_filter *filter;
  485. filter = &peer->filter[afi][safi];
  486. #define FILTER_EXIST_WARN(F,f,filter) \
  487. if (BGP_DEBUG (update, UPDATE_IN) \
  488. && !(F ## _IN (filter))) \
  489. plog_warn (peer->log, "%s: Could not find configured input %s-list %s!", \
  490. peer->host, #f, F ## _IN_NAME(filter));
  491. if (DISTRIBUTE_IN_NAME (filter)) {
  492. FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
  493. if (access_list_apply (DISTRIBUTE_IN (filter), p) == FILTER_DENY)
  494. return FILTER_DENY;
  495. }
  496. if (PREFIX_LIST_IN_NAME (filter)) {
  497. FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
  498. if (prefix_list_apply (PREFIX_LIST_IN (filter), p) == PREFIX_DENY)
  499. return FILTER_DENY;
  500. }
  501. if (FILTER_LIST_IN_NAME (filter)) {
  502. FILTER_EXIST_WARN(FILTER_LIST, as, filter);
  503. if (as_list_apply (FILTER_LIST_IN (filter), attr->aspath)== AS_FILTER_DENY)
  504. return FILTER_DENY;
  505. }
  506. return FILTER_PERMIT;
  507. #undef FILTER_EXIST_WARN
  508. }
  509. static enum filter_type
  510. bgp_output_filter (struct peer *peer, struct prefix *p, struct attr *attr,
  511. afi_t afi, safi_t safi)
  512. {
  513. struct bgp_filter *filter;
  514. filter = &peer->filter[afi][safi];
  515. #define FILTER_EXIST_WARN(F,f,filter) \
  516. if (BGP_DEBUG (update, UPDATE_OUT) \
  517. && !(F ## _OUT (filter))) \
  518. plog_warn (peer->log, "%s: Could not find configured output %s-list %s!", \
  519. peer->host, #f, F ## _OUT_NAME(filter));
  520. if (DISTRIBUTE_OUT_NAME (filter)) {
  521. FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
  522. if (access_list_apply (DISTRIBUTE_OUT (filter), p) == FILTER_DENY)
  523. return FILTER_DENY;
  524. }
  525. if (PREFIX_LIST_OUT_NAME (filter)) {
  526. FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
  527. if (prefix_list_apply (PREFIX_LIST_OUT (filter), p) == PREFIX_DENY)
  528. return FILTER_DENY;
  529. }
  530. if (FILTER_LIST_OUT_NAME (filter)) {
  531. FILTER_EXIST_WARN(FILTER_LIST, as, filter);
  532. if (as_list_apply (FILTER_LIST_OUT (filter), attr->aspath) == AS_FILTER_DENY)
  533. return FILTER_DENY;
  534. }
  535. return FILTER_PERMIT;
  536. #undef FILTER_EXIST_WARN
  537. }
  538. /* If community attribute includes no_export then return 1. */
  539. static int
  540. bgp_community_filter (struct peer *peer, struct attr *attr)
  541. {
  542. if (attr->community)
  543. {
  544. /* NO_ADVERTISE check. */
  545. if (community_include (attr->community, COMMUNITY_NO_ADVERTISE))
  546. return 1;
  547. /* NO_EXPORT check. */
  548. if (peer->sort == BGP_PEER_EBGP &&
  549. community_include (attr->community, COMMUNITY_NO_EXPORT))
  550. return 1;
  551. /* NO_EXPORT_SUBCONFED check. */
  552. if (peer->sort == BGP_PEER_EBGP
  553. || peer->sort == BGP_PEER_CONFED)
  554. if (community_include (attr->community, COMMUNITY_NO_EXPORT_SUBCONFED))
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. /* Route reflection loop check. */
  560. static int
  561. bgp_cluster_filter (struct peer *peer, struct attr *attr)
  562. {
  563. struct in_addr cluster_id;
  564. if (attr->extra && attr->extra->cluster)
  565. {
  566. if (peer->bgp->config & BGP_CONFIG_CLUSTER_ID)
  567. cluster_id = peer->bgp->cluster_id;
  568. else
  569. cluster_id = peer->bgp->router_id;
  570. if (cluster_loop_check (attr->extra->cluster, cluster_id))
  571. return 1;
  572. }
  573. return 0;
  574. }
  575. static int
  576. bgp_input_modifier (struct peer *peer, struct prefix *p, struct attr *attr,
  577. afi_t afi, safi_t safi)
  578. {
  579. struct bgp_filter *filter;
  580. struct bgp_info info;
  581. route_map_result_t ret;
  582. filter = &peer->filter[afi][safi];
  583. /* Apply default weight value. */
  584. if (peer->weight)
  585. (bgp_attr_extra_get (attr))->weight = peer->weight;
  586. /* Route map apply. */
  587. if (ROUTE_MAP_IN_NAME (filter))
  588. {
  589. /* Duplicate current value to new strucutre for modification. */
  590. info.peer = peer;
  591. info.attr = attr;
  592. SET_FLAG (peer->rmap_type, PEER_RMAP_TYPE_IN);
  593. /* Apply BGP route map to the attribute. */
  594. ret = route_map_apply (ROUTE_MAP_IN (filter), p, RMAP_BGP, &info);
  595. peer->rmap_type = 0;
  596. if (ret == RMAP_DENYMATCH)
  597. /* caller has multiple error paths with bgp_attr_flush() */
  598. return RMAP_DENY;
  599. }
  600. return RMAP_PERMIT;
  601. }
  602. static int
  603. bgp_export_modifier (struct peer *rsclient, struct peer *peer,
  604. struct prefix *p, struct attr *attr, afi_t afi, safi_t safi)
  605. {
  606. struct bgp_filter *filter;
  607. struct bgp_info info;
  608. route_map_result_t ret;
  609. filter = &peer->filter[afi][safi];
  610. /* Route map apply. */
  611. if (ROUTE_MAP_EXPORT_NAME (filter))
  612. {
  613. /* Duplicate current value to new strucutre for modification. */
  614. info.peer = rsclient;
  615. info.attr = attr;
  616. SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_EXPORT);
  617. /* Apply BGP route map to the attribute. */
  618. ret = route_map_apply (ROUTE_MAP_EXPORT (filter), p, RMAP_BGP, &info);
  619. rsclient->rmap_type = 0;
  620. if (ret == RMAP_DENYMATCH)
  621. {
  622. /* Free newly generated AS path and community by route-map. */
  623. bgp_attr_flush (attr);
  624. return RMAP_DENY;
  625. }
  626. }
  627. return RMAP_PERMIT;
  628. }
  629. static int
  630. bgp_import_modifier (struct peer *rsclient, struct peer *peer,
  631. struct prefix *p, struct attr *attr, afi_t afi, safi_t safi)
  632. {
  633. struct bgp_filter *filter;
  634. struct bgp_info info;
  635. route_map_result_t ret;
  636. filter = &rsclient->filter[afi][safi];
  637. /* Apply default weight value. */
  638. if (peer->weight)
  639. (bgp_attr_extra_get (attr))->weight = peer->weight;
  640. /* Route map apply. */
  641. if (ROUTE_MAP_IMPORT_NAME (filter))
  642. {
  643. /* Duplicate current value to new strucutre for modification. */
  644. info.peer = peer;
  645. info.attr = attr;
  646. SET_FLAG (peer->rmap_type, PEER_RMAP_TYPE_IMPORT);
  647. /* Apply BGP route map to the attribute. */
  648. ret = route_map_apply (ROUTE_MAP_IMPORT (filter), p, RMAP_BGP, &info);
  649. peer->rmap_type = 0;
  650. if (ret == RMAP_DENYMATCH)
  651. {
  652. /* Free newly generated AS path and community by route-map. */
  653. bgp_attr_flush (attr);
  654. return RMAP_DENY;
  655. }
  656. }
  657. return RMAP_PERMIT;
  658. }
  659. static int
  660. bgp_announce_check (struct bgp_info *ri, struct peer *peer, struct prefix *p,
  661. struct attr *attr, afi_t afi, safi_t safi)
  662. {
  663. int ret;
  664. char buf[SU_ADDRSTRLEN];
  665. struct bgp_filter *filter;
  666. struct peer *from;
  667. struct bgp *bgp;
  668. int transparent;
  669. int reflect;
  670. struct attr *riattr;
  671. from = ri->peer;
  672. filter = &peer->filter[afi][safi];
  673. bgp = peer->bgp;
  674. riattr = bgp_info_mpath_count (ri) ? bgp_info_mpath_attr (ri) : ri->attr;
  675. if (DISABLE_BGP_ANNOUNCE)
  676. return 0;
  677. /* Do not send announces to RS-clients from the 'normal' bgp_table. */
  678. if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  679. return 0;
  680. /* Do not send back route to sender. */
  681. if (from == peer)
  682. return 0;
  683. /* Aggregate-address suppress check. */
  684. if (ri->extra && ri->extra->suppress)
  685. if (! UNSUPPRESS_MAP_NAME (filter))
  686. return 0;
  687. /* Default route check. */
  688. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE))
  689. {
  690. if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
  691. return 0;
  692. else if (p->family == AF_INET6 && p->prefixlen == 0)
  693. return 0;
  694. }
  695. /* Transparency check. */
  696. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
  697. && CHECK_FLAG (from->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  698. transparent = 1;
  699. else
  700. transparent = 0;
  701. /* If community is not disabled check the no-export and local. */
  702. if (! transparent && bgp_community_filter (peer, riattr))
  703. return 0;
  704. /* If the attribute has originator-id and it is same as remote
  705. peer's id. */
  706. if (riattr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID))
  707. {
  708. if (IPV4_ADDR_SAME (&peer->remote_id, &riattr->extra->originator_id))
  709. {
  710. if (BGP_DEBUG (filter, FILTER))
  711. zlog (peer->log, LOG_DEBUG,
  712. "%s [Update:SEND] %s/%d originator-id is same as remote router-id",
  713. peer->host,
  714. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  715. p->prefixlen);
  716. return 0;
  717. }
  718. }
  719. /* ORF prefix-list filter check */
  720. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
  721. && (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
  722. || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
  723. if (peer->orf_plist[afi][safi])
  724. {
  725. if (prefix_list_apply (peer->orf_plist[afi][safi], p) == PREFIX_DENY)
  726. return 0;
  727. }
  728. /* Output filter check. */
  729. if (bgp_output_filter (peer, p, riattr, afi, safi) == FILTER_DENY)
  730. {
  731. if (BGP_DEBUG (filter, FILTER))
  732. zlog (peer->log, LOG_DEBUG,
  733. "%s [Update:SEND] %s/%d is filtered",
  734. peer->host,
  735. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  736. p->prefixlen);
  737. return 0;
  738. }
  739. #ifdef BGP_SEND_ASPATH_CHECK
  740. /* AS path loop check. */
  741. if (aspath_loop_check (riattr->aspath, peer->as))
  742. {
  743. if (BGP_DEBUG (filter, FILTER))
  744. zlog (peer->log, LOG_DEBUG,
  745. "%s [Update:SEND] suppress announcement to peer AS %u is AS path.",
  746. peer->host, peer->as);
  747. return 0;
  748. }
  749. /* If we're a CONFED we need to loop check the CONFED ID too */
  750. if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
  751. {
  752. if (aspath_loop_check(riattr->aspath, bgp->confed_id))
  753. {
  754. if (BGP_DEBUG (filter, FILTER))
  755. zlog (peer->log, LOG_DEBUG,
  756. "%s [Update:SEND] suppress announcement to peer AS %u is AS path.",
  757. peer->host,
  758. bgp->confed_id);
  759. return 0;
  760. }
  761. }
  762. #endif /* BGP_SEND_ASPATH_CHECK */
  763. /* Route-Reflect check. */
  764. if (from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
  765. reflect = 1;
  766. else
  767. reflect = 0;
  768. /* IBGP reflection check. */
  769. if (reflect)
  770. {
  771. /* A route from a Client peer. */
  772. if (CHECK_FLAG (from->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
  773. {
  774. /* Reflect to all the Non-Client peers and also to the
  775. Client peers other than the originator. Originator check
  776. is already done. So there is noting to do. */
  777. /* no bgp client-to-client reflection check. */
  778. if (bgp_flag_check (bgp, BGP_FLAG_NO_CLIENT_TO_CLIENT))
  779. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
  780. return 0;
  781. }
  782. else
  783. {
  784. /* A route from a Non-client peer. Reflect to all other
  785. clients. */
  786. if (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
  787. return 0;
  788. }
  789. }
  790. /* For modify attribute, copy it to temporary structure. */
  791. bgp_attr_dup (attr, riattr);
  792. /* If local-preference is not set. */
  793. if ((peer->sort == BGP_PEER_IBGP
  794. || peer->sort == BGP_PEER_CONFED)
  795. && (! (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))))
  796. {
  797. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
  798. attr->local_pref = bgp->default_local_pref;
  799. }
  800. /* If originator-id is not set and the route is to be reflected,
  801. set the originator id */
  802. if (peer && from && peer->sort == BGP_PEER_IBGP &&
  803. from->sort == BGP_PEER_IBGP &&
  804. (! (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))))
  805. {
  806. attr->extra = bgp_attr_extra_get(attr);
  807. IPV4_ADDR_COPY(&(attr->extra->originator_id), &(from->remote_id));
  808. SET_FLAG(attr->flag, BGP_ATTR_ORIGINATOR_ID);
  809. }
  810. /* Remove MED if its an EBGP peer - will get overwritten by route-maps */
  811. if (peer->sort == BGP_PEER_EBGP
  812. && attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  813. {
  814. if (ri->peer != bgp->peer_self && ! transparent
  815. && ! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED))
  816. attr->flag &= ~(ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC));
  817. }
  818. #define NEXTHOP_IS_V4 (\
  819. (safi != SAFI_ENCAP && p->family == AF_INET) || \
  820. (safi == SAFI_ENCAP && attr->extra->mp_nexthop_len == 4))
  821. #define NEXTHOP_IS_V6 (\
  822. (safi != SAFI_ENCAP && p->family == AF_INET6) || \
  823. (safi == SAFI_ENCAP && attr->extra->mp_nexthop_len == 16))
  824. /* next-hop-set */
  825. if (transparent
  826. || (reflect && ! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_SELF_ALL))
  827. || (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)
  828. && ((NEXTHOP_IS_V4 && attr->nexthop.s_addr)
  829. || (NEXTHOP_IS_V6 &&
  830. ! IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
  831. )))
  832. {
  833. /* NEXT-HOP Unchanged. */
  834. }
  835. else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_SELF)
  836. || (NEXTHOP_IS_V4 && attr->nexthop.s_addr == 0)
  837. || (NEXTHOP_IS_V6 &&
  838. IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
  839. || (peer->sort == BGP_PEER_EBGP
  840. && (bgp_multiaccess_check_v4 (attr->nexthop, peer) == 0)))
  841. {
  842. /* Set IPv4 nexthop. */
  843. if (NEXTHOP_IS_V4)
  844. {
  845. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  846. memcpy (&attr->extra->mp_nexthop_global_in, &peer->nexthop.v4,
  847. IPV4_MAX_BYTELEN);
  848. else
  849. memcpy (&attr->nexthop, &peer->nexthop.v4, IPV4_MAX_BYTELEN);
  850. }
  851. /* Set IPv6 nexthop. */
  852. if (NEXTHOP_IS_V6)
  853. {
  854. /* IPv6 global nexthop must be included. */
  855. memcpy (&attr->extra->mp_nexthop_global, &peer->nexthop.v6_global,
  856. IPV6_MAX_BYTELEN);
  857. attr->extra->mp_nexthop_len = 16;
  858. }
  859. }
  860. if (p->family == AF_INET6 && safi != SAFI_ENCAP)
  861. {
  862. /* Left nexthop_local unchanged if so configured. */
  863. if ( CHECK_FLAG (peer->af_flags[afi][safi],
  864. PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED) )
  865. {
  866. if ( IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_local) )
  867. attr->extra->mp_nexthop_len=32;
  868. else
  869. attr->extra->mp_nexthop_len=16;
  870. }
  871. /* Default nexthop_local treatment for non-RS-Clients */
  872. else
  873. {
  874. /* Link-local address should not be transit to different peer. */
  875. attr->extra->mp_nexthop_len = 16;
  876. /* Set link-local address for shared network peer. */
  877. if (peer->shared_network
  878. && ! IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_local))
  879. {
  880. memcpy (&attr->extra->mp_nexthop_local, &peer->nexthop.v6_local,
  881. IPV6_MAX_BYTELEN);
  882. attr->extra->mp_nexthop_len = 32;
  883. }
  884. /* If bgpd act as BGP-4+ route-reflector, do not send link-local
  885. address.*/
  886. if (reflect)
  887. attr->extra->mp_nexthop_len = 16;
  888. /* If BGP-4+ link-local nexthop is not link-local nexthop. */
  889. if (! IN6_IS_ADDR_LINKLOCAL (&peer->nexthop.v6_local))
  890. attr->extra->mp_nexthop_len = 16;
  891. }
  892. }
  893. /* If this is EBGP peer and remove-private-AS is set. */
  894. if (peer->sort == BGP_PEER_EBGP
  895. && peer_af_flag_check (peer, afi, safi, PEER_FLAG_REMOVE_PRIVATE_AS)
  896. && aspath_private_as_check (attr->aspath))
  897. attr->aspath = aspath_empty_get ();
  898. /* Route map & unsuppress-map apply. */
  899. if (ROUTE_MAP_OUT_NAME (filter)
  900. || (ri->extra && ri->extra->suppress) )
  901. {
  902. struct bgp_info info;
  903. struct attr dummy_attr;
  904. struct attr_extra dummy_extra;
  905. dummy_attr.extra = &dummy_extra;
  906. info.peer = peer;
  907. info.attr = attr;
  908. /* The route reflector is not allowed to modify the attributes
  909. of the reflected IBGP routes, unless configured to allow it */
  910. if ((from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP) &&
  911. !bgp_flag_check(bgp, BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY))
  912. {
  913. bgp_attr_dup (&dummy_attr, attr);
  914. info.attr = &dummy_attr;
  915. }
  916. SET_FLAG (peer->rmap_type, PEER_RMAP_TYPE_OUT);
  917. if (ri->extra && ri->extra->suppress)
  918. ret = route_map_apply (UNSUPPRESS_MAP (filter), p, RMAP_BGP, &info);
  919. else
  920. ret = route_map_apply (ROUTE_MAP_OUT (filter), p, RMAP_BGP, &info);
  921. peer->rmap_type = 0;
  922. if (ret == RMAP_DENYMATCH)
  923. {
  924. bgp_attr_flush (attr);
  925. return 0;
  926. }
  927. }
  928. return 1;
  929. }
  930. static int
  931. bgp_announce_check_rsclient (struct bgp_info *ri, struct peer *rsclient,
  932. struct prefix *p, struct attr *attr, afi_t afi, safi_t safi)
  933. {
  934. int ret;
  935. char buf[SU_ADDRSTRLEN];
  936. struct bgp_filter *filter;
  937. struct bgp_info info;
  938. struct peer *from;
  939. struct attr *riattr;
  940. from = ri->peer;
  941. filter = &rsclient->filter[afi][safi];
  942. riattr = bgp_info_mpath_count (ri) ? bgp_info_mpath_attr (ri) : ri->attr;
  943. if (DISABLE_BGP_ANNOUNCE)
  944. return 0;
  945. /* Do not send back route to sender. */
  946. if (from == rsclient)
  947. return 0;
  948. /* Aggregate-address suppress check. */
  949. if (ri->extra && ri->extra->suppress)
  950. if (! UNSUPPRESS_MAP_NAME (filter))
  951. return 0;
  952. /* Default route check. */
  953. if (CHECK_FLAG (rsclient->af_sflags[afi][safi],
  954. PEER_STATUS_DEFAULT_ORIGINATE))
  955. {
  956. if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
  957. return 0;
  958. else if (p->family == AF_INET6 && p->prefixlen == 0)
  959. return 0;
  960. }
  961. /* If the attribute has originator-id and it is same as remote
  962. peer's id. */
  963. if (riattr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID))
  964. {
  965. if (IPV4_ADDR_SAME (&rsclient->remote_id,
  966. &riattr->extra->originator_id))
  967. {
  968. if (BGP_DEBUG (filter, FILTER))
  969. zlog (rsclient->log, LOG_DEBUG,
  970. "%s [Update:SEND] %s/%d originator-id is same as remote router-id",
  971. rsclient->host,
  972. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  973. p->prefixlen);
  974. return 0;
  975. }
  976. }
  977. /* ORF prefix-list filter check */
  978. if (CHECK_FLAG (rsclient->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
  979. && (CHECK_FLAG (rsclient->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
  980. || CHECK_FLAG (rsclient->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
  981. if (rsclient->orf_plist[afi][safi])
  982. {
  983. if (prefix_list_apply (rsclient->orf_plist[afi][safi], p) == PREFIX_DENY)
  984. return 0;
  985. }
  986. /* Output filter check. */
  987. if (bgp_output_filter (rsclient, p, riattr, afi, safi) == FILTER_DENY)
  988. {
  989. if (BGP_DEBUG (filter, FILTER))
  990. zlog (rsclient->log, LOG_DEBUG,
  991. "%s [Update:SEND] %s/%d is filtered",
  992. rsclient->host,
  993. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  994. p->prefixlen);
  995. return 0;
  996. }
  997. #ifdef BGP_SEND_ASPATH_CHECK
  998. /* AS path loop check. */
  999. if (aspath_loop_check (riattr->aspath, rsclient->as))
  1000. {
  1001. if (BGP_DEBUG (filter, FILTER))
  1002. zlog (rsclient->log, LOG_DEBUG,
  1003. "%s [Update:SEND] suppress announcement to peer AS %u is AS path.",
  1004. rsclient->host, rsclient->as);
  1005. return 0;
  1006. }
  1007. #endif /* BGP_SEND_ASPATH_CHECK */
  1008. /* For modify attribute, copy it to temporary structure. */
  1009. bgp_attr_dup (attr, riattr);
  1010. /* next-hop-set */
  1011. if ((p->family == AF_INET && attr->nexthop.s_addr == 0)
  1012. || (p->family == AF_INET6 &&
  1013. IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
  1014. )
  1015. {
  1016. /* Set IPv4 nexthop. */
  1017. if (p->family == AF_INET)
  1018. {
  1019. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  1020. memcpy (&attr->extra->mp_nexthop_global_in, &rsclient->nexthop.v4,
  1021. IPV4_MAX_BYTELEN);
  1022. else
  1023. memcpy (&attr->nexthop, &rsclient->nexthop.v4, IPV4_MAX_BYTELEN);
  1024. }
  1025. /* Set IPv6 nexthop. */
  1026. if (p->family == AF_INET6)
  1027. {
  1028. /* IPv6 global nexthop must be included. */
  1029. memcpy (&attr->extra->mp_nexthop_global, &rsclient->nexthop.v6_global,
  1030. IPV6_MAX_BYTELEN);
  1031. attr->extra->mp_nexthop_len = 16;
  1032. }
  1033. }
  1034. if (p->family == AF_INET6)
  1035. {
  1036. struct attr_extra *attre = attr->extra;
  1037. /* Left nexthop_local unchanged if so configured. */
  1038. if ( CHECK_FLAG (rsclient->af_flags[afi][safi],
  1039. PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED) )
  1040. {
  1041. if ( IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local) )
  1042. attre->mp_nexthop_len=32;
  1043. else
  1044. attre->mp_nexthop_len=16;
  1045. }
  1046. /* Default nexthop_local treatment for RS-Clients */
  1047. else
  1048. {
  1049. /* Announcer and RS-Client are both in the same network */
  1050. if (rsclient->shared_network && from->shared_network &&
  1051. (rsclient->ifindex == from->ifindex))
  1052. {
  1053. if ( IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local) )
  1054. attre->mp_nexthop_len=32;
  1055. else
  1056. attre->mp_nexthop_len=16;
  1057. }
  1058. /* Set link-local address for shared network peer. */
  1059. else if (rsclient->shared_network
  1060. && IN6_IS_ADDR_LINKLOCAL (&rsclient->nexthop.v6_local))
  1061. {
  1062. memcpy (&attre->mp_nexthop_local, &rsclient->nexthop.v6_local,
  1063. IPV6_MAX_BYTELEN);
  1064. attre->mp_nexthop_len = 32;
  1065. }
  1066. else
  1067. attre->mp_nexthop_len = 16;
  1068. }
  1069. }
  1070. /* If this is EBGP peer and remove-private-AS is set. */
  1071. if (rsclient->sort == BGP_PEER_EBGP
  1072. && peer_af_flag_check (rsclient, afi, safi, PEER_FLAG_REMOVE_PRIVATE_AS)
  1073. && aspath_private_as_check (attr->aspath))
  1074. attr->aspath = aspath_empty_get ();
  1075. /* Route map & unsuppress-map apply. */
  1076. if (ROUTE_MAP_OUT_NAME (filter) || (ri->extra && ri->extra->suppress) )
  1077. {
  1078. info.peer = rsclient;
  1079. info.attr = attr;
  1080. SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_OUT);
  1081. if (ri->extra && ri->extra->suppress)
  1082. ret = route_map_apply (UNSUPPRESS_MAP (filter), p, RMAP_BGP, &info);
  1083. else
  1084. ret = route_map_apply (ROUTE_MAP_OUT (filter), p, RMAP_BGP, &info);
  1085. rsclient->rmap_type = 0;
  1086. if (ret == RMAP_DENYMATCH)
  1087. {
  1088. bgp_attr_flush (attr);
  1089. return 0;
  1090. }
  1091. }
  1092. return 1;
  1093. }
  1094. struct bgp_info_pair
  1095. {
  1096. struct bgp_info *old;
  1097. struct bgp_info *new;
  1098. };
  1099. static void
  1100. bgp_best_selection (struct bgp *bgp, struct bgp_node *rn,
  1101. struct bgp_info_pair *result,
  1102. afi_t afi, safi_t safi)
  1103. {
  1104. struct bgp_info *new_select;
  1105. struct bgp_info *old_select;
  1106. struct bgp_info *ri;
  1107. struct bgp_info *ri1;
  1108. struct bgp_info *ri2;
  1109. struct bgp_info *nextri = NULL;
  1110. int cmpret, do_mpath;
  1111. struct list mp_list;
  1112. result->old = result->new = NULL;
  1113. if (rn->info == NULL)
  1114. {
  1115. char buf[PREFIX_STRLEN];
  1116. zlog_warn ("%s: Called for route_node %s with no routing entries!",
  1117. __func__,
  1118. prefix2str (&(bgp_node_to_rnode (rn)->p), buf, sizeof(buf)));
  1119. return;
  1120. }
  1121. bgp_mp_list_init (&mp_list);
  1122. do_mpath = bgp_mpath_is_configured (bgp, afi, safi);
  1123. /* bgp deterministic-med */
  1124. new_select = NULL;
  1125. if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
  1126. for (ri1 = rn->info; ri1; ri1 = ri1->next)
  1127. {
  1128. if (CHECK_FLAG (ri1->flags, BGP_INFO_DMED_CHECK))
  1129. continue;
  1130. if (BGP_INFO_HOLDDOWN (ri1))
  1131. continue;
  1132. if (ri1->peer && ri1->peer != bgp->peer_self)
  1133. if (ri1->peer->status != Established)
  1134. continue;
  1135. new_select = ri1;
  1136. if (do_mpath)
  1137. bgp_mp_list_add (&mp_list, ri1);
  1138. old_select = CHECK_FLAG (ri1->flags, BGP_INFO_SELECTED) ? ri1 : NULL;
  1139. if (ri1->next)
  1140. for (ri2 = ri1->next; ri2; ri2 = ri2->next)
  1141. {
  1142. if (CHECK_FLAG (ri2->flags, BGP_INFO_DMED_CHECK))
  1143. continue;
  1144. if (BGP_INFO_HOLDDOWN (ri2))
  1145. continue;
  1146. if (ri2->peer &&
  1147. ri2->peer != bgp->peer_self &&
  1148. !CHECK_FLAG (ri2->peer->sflags, PEER_STATUS_NSF_WAIT))
  1149. if (ri2->peer->status != Established)
  1150. continue;
  1151. if (aspath_cmp_left (ri1->attr->aspath, ri2->attr->aspath)
  1152. || aspath_cmp_left_confed (ri1->attr->aspath,
  1153. ri2->attr->aspath))
  1154. {
  1155. if (CHECK_FLAG (ri2->flags, BGP_INFO_SELECTED))
  1156. old_select = ri2;
  1157. if ((cmpret = bgp_info_cmp (bgp, ri2, new_select, afi, safi))
  1158. == -1)
  1159. {
  1160. bgp_info_unset_flag (rn, new_select, BGP_INFO_DMED_SELECTED);
  1161. new_select = ri2;
  1162. }
  1163. if (do_mpath)
  1164. {
  1165. if (cmpret != 0)
  1166. bgp_mp_list_clear (&mp_list);
  1167. if (cmpret == 0 || cmpret == -1)
  1168. bgp_mp_list_add (&mp_list, ri2);
  1169. }
  1170. bgp_info_set_flag (rn, ri2, BGP_INFO_DMED_CHECK);
  1171. }
  1172. }
  1173. bgp_info_set_flag (rn, new_select, BGP_INFO_DMED_CHECK);
  1174. bgp_info_set_flag (rn, new_select, BGP_INFO_DMED_SELECTED);
  1175. bgp_info_mpath_update (rn, new_select, old_select, &mp_list, afi, safi);
  1176. bgp_mp_list_clear (&mp_list);
  1177. }
  1178. /* Check old selected route and new selected route. */
  1179. old_select = NULL;
  1180. new_select = NULL;
  1181. for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1); ri = nextri)
  1182. {
  1183. if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))
  1184. old_select = ri;
  1185. if (BGP_INFO_HOLDDOWN (ri))
  1186. {
  1187. /* reap REMOVED routes, if needs be
  1188. * selected route must stay for a while longer though
  1189. */
  1190. if (CHECK_FLAG (ri->flags, BGP_INFO_REMOVED)
  1191. && (ri != old_select))
  1192. bgp_info_reap (rn, ri);
  1193. continue;
  1194. }
  1195. if (ri->peer &&
  1196. ri->peer != bgp->peer_self &&
  1197. !CHECK_FLAG (ri->peer->sflags, PEER_STATUS_NSF_WAIT))
  1198. if (ri->peer->status != Established)
  1199. continue;
  1200. if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED)
  1201. && (! CHECK_FLAG (ri->flags, BGP_INFO_DMED_SELECTED)))
  1202. {
  1203. bgp_info_unset_flag (rn, ri, BGP_INFO_DMED_CHECK);
  1204. continue;
  1205. }
  1206. bgp_info_unset_flag (rn, ri, BGP_INFO_DMED_CHECK);
  1207. bgp_info_unset_flag (rn, ri, BGP_INFO_DMED_SELECTED);
  1208. if ((cmpret = bgp_info_cmp (bgp, ri, new_select, afi, safi)) == -1)
  1209. {
  1210. if (do_mpath && bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
  1211. bgp_mp_dmed_deselect (new_select);
  1212. new_select = ri;
  1213. }
  1214. else if (cmpret == 1 && do_mpath
  1215. && bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
  1216. bgp_mp_dmed_deselect (ri);
  1217. if (do_mpath)
  1218. {
  1219. if (cmpret != 0)
  1220. bgp_mp_list_clear (&mp_list);
  1221. if (cmpret == 0 || cmpret == -1)
  1222. bgp_mp_list_add (&mp_list, ri);
  1223. }
  1224. }
  1225. if (!bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
  1226. bgp_info_mpath_update (rn, new_select, old_select, &mp_list, afi, safi);
  1227. bgp_info_mpath_aggregate_update (new_select, old_select);
  1228. bgp_mp_list_clear (&mp_list);
  1229. result->old = old_select;
  1230. result->new = new_select;
  1231. return;
  1232. }
  1233. static int
  1234. bgp_process_announce_selected (struct peer *peer, struct bgp_info *selected,
  1235. struct bgp_node *rn, afi_t afi, safi_t safi)
  1236. {
  1237. struct prefix *p;
  1238. struct attr attr;
  1239. struct attr_extra extra;
  1240. memset (&attr, 0, sizeof(struct attr));
  1241. memset (&extra, 0, sizeof(struct attr_extra));
  1242. p = &rn->p;
  1243. /* Announce route to Established peer. */
  1244. if (peer->status != Established)
  1245. return 0;
  1246. /* Address family configuration check. */
  1247. if (! peer->afc_nego[afi][safi])
  1248. return 0;
  1249. /* First update is deferred until ORF or ROUTE-REFRESH is received */
  1250. if (CHECK_FLAG (peer->af_sflags[afi][safi],
  1251. PEER_STATUS_ORF_WAIT_REFRESH))
  1252. return 0;
  1253. /* It's initialized in bgp_announce_[check|check_rsclient]() */
  1254. attr.extra = &extra;
  1255. switch (bgp_node_table (rn)->type)
  1256. {
  1257. case BGP_TABLE_MAIN:
  1258. /* Announcement to peer->conf. If the route is filtered,
  1259. withdraw it. */
  1260. if (selected && bgp_announce_check (selected, peer, p, &attr, afi, safi))
  1261. bgp_adj_out_set (rn, peer, p, &attr, afi, safi, selected);
  1262. else
  1263. bgp_adj_out_unset (rn, peer, p, afi, safi);
  1264. break;
  1265. case BGP_TABLE_RSCLIENT:
  1266. /* Announcement to peer->conf. If the route is filtered,
  1267. withdraw it. */
  1268. if (selected &&
  1269. bgp_announce_check_rsclient (selected, peer, p, &attr, afi, safi))
  1270. bgp_adj_out_set (rn, peer, p, &attr, afi, safi, selected);
  1271. else
  1272. bgp_adj_out_unset (rn, peer, p, afi, safi);
  1273. break;
  1274. }
  1275. bgp_attr_flush (&attr);
  1276. return 0;
  1277. }
  1278. struct bgp_process_queue
  1279. {
  1280. struct bgp *bgp;
  1281. struct bgp_node *rn;
  1282. afi_t afi;
  1283. safi_t safi;
  1284. };
  1285. static wq_item_status
  1286. bgp_process_rsclient (struct work_queue *wq, void *data)
  1287. {
  1288. struct bgp_process_queue *pq = data;
  1289. struct bgp *bgp = pq->bgp;
  1290. struct bgp_node *rn = pq->rn;
  1291. afi_t afi = pq->afi;
  1292. safi_t safi = pq->safi;
  1293. struct bgp_info *new_select;
  1294. struct bgp_info *old_select;
  1295. struct bgp_info_pair old_and_new;
  1296. struct listnode *node, *nnode;
  1297. struct peer *rsclient = bgp_node_table (rn)->owner;
  1298. /* Best path selection. */
  1299. bgp_best_selection (bgp, rn, &old_and_new, afi, safi);
  1300. new_select = old_and_new.new;
  1301. old_select = old_and_new.old;
  1302. if (CHECK_FLAG (rsclient->sflags, PEER_STATUS_GROUP))
  1303. {
  1304. if (rsclient->group)
  1305. for (ALL_LIST_ELEMENTS (rsclient->group->peer, node, nnode, rsclient))
  1306. {
  1307. /* Nothing to do. */
  1308. if (old_select && old_select == new_select)
  1309. if (!CHECK_FLAG (old_select->flags, BGP_INFO_ATTR_CHANGED))
  1310. continue;
  1311. if (old_select)
  1312. bgp_info_unset_flag (rn, old_select, BGP_INFO_SELECTED);
  1313. if (new_select)
  1314. {
  1315. bgp_info_set_flag (rn, new_select, BGP_INFO_SELECTED);
  1316. bgp_info_unset_flag (rn, new_select, BGP_INFO_ATTR_CHANGED);
  1317. UNSET_FLAG (new_select->flags, BGP_INFO_MULTIPATH_CHG);
  1318. }
  1319. bgp_process_announce_selected (rsclient, new_select, rn,
  1320. afi, safi);
  1321. }
  1322. }
  1323. else
  1324. {
  1325. if (old_select)
  1326. bgp_info_unset_flag (rn, old_select, BGP_INFO_SELECTED);
  1327. if (new_select)
  1328. {
  1329. bgp_info_set_flag (rn, new_select, BGP_INFO_SELECTED);
  1330. bgp_info_unset_flag (rn, new_select, BGP_INFO_ATTR_CHANGED);
  1331. UNSET_FLAG (new_select->flags, BGP_INFO_MULTIPATH_CHG);
  1332. }
  1333. bgp_process_announce_selected (rsclient, new_select, rn, afi, safi);
  1334. }
  1335. if (old_select && CHECK_FLAG (old_select->flags, BGP_INFO_REMOVED))
  1336. bgp_info_reap (rn, old_select);
  1337. UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
  1338. return WQ_SUCCESS;
  1339. }
  1340. static wq_item_status
  1341. bgp_process_main (struct work_queue *wq, void *data)
  1342. {
  1343. struct bgp_process_queue *pq = data;
  1344. struct bgp *bgp = pq->bgp;
  1345. struct bgp_node *rn = pq->rn;
  1346. afi_t afi = pq->afi;
  1347. safi_t safi = pq->safi;
  1348. struct prefix *p = &rn->p;
  1349. struct bgp_info *new_select;
  1350. struct bgp_info *old_select;
  1351. struct bgp_info_pair old_and_new;
  1352. struct listnode *node, *nnode;
  1353. struct peer *peer;
  1354. /* Best path selection. */
  1355. bgp_best_selection (bgp, rn, &old_and_new, afi, safi);
  1356. old_select = old_and_new.old;
  1357. new_select = old_and_new.new;
  1358. /* Nothing to do. */
  1359. if (old_select && old_select == new_select
  1360. && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR))
  1361. {
  1362. if (! CHECK_FLAG (old_select->flags, BGP_INFO_ATTR_CHANGED))
  1363. {
  1364. if (CHECK_FLAG (old_select->flags, BGP_INFO_IGP_CHANGED) ||
  1365. CHECK_FLAG (old_select->flags, BGP_INFO_MULTIPATH_CHG))
  1366. bgp_zebra_announce (p, old_select, bgp, safi);
  1367. UNSET_FLAG (old_select->flags, BGP_INFO_MULTIPATH_CHG);
  1368. UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
  1369. return WQ_SUCCESS;
  1370. }
  1371. }
  1372. /* If the user did "clear ip bgp prefix x.x.x.x" this flag will be set */
  1373. UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
  1374. if (old_select)
  1375. bgp_info_unset_flag (rn, old_select, BGP_INFO_SELECTED);
  1376. if (new_select)
  1377. {
  1378. bgp_info_set_flag (rn, new_select, BGP_INFO_SELECTED);
  1379. bgp_info_unset_flag (rn, new_select, BGP_INFO_ATTR_CHANGED);
  1380. UNSET_FLAG (new_select->flags, BGP_INFO_MULTIPATH_CHG);
  1381. }
  1382. /* Check each BGP peer. */
  1383. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  1384. {
  1385. bgp_process_announce_selected (peer, new_select, rn, afi, safi);
  1386. }
  1387. /* FIB update. */
  1388. if ((safi == SAFI_UNICAST || safi == SAFI_MULTICAST) && (! bgp->name &&
  1389. ! bgp_option_check (BGP_OPT_NO_FIB)))
  1390. {
  1391. if (new_select
  1392. && new_select->type == ZEBRA_ROUTE_BGP
  1393. && new_select->sub_type == BGP_ROUTE_NORMAL)
  1394. bgp_zebra_announce (p, new_select, bgp, safi);
  1395. else
  1396. {
  1397. /* Withdraw the route from the kernel. */
  1398. if (old_select
  1399. && old_select->type == ZEBRA_ROUTE_BGP
  1400. && old_select->sub_type == BGP_ROUTE_NORMAL)
  1401. bgp_zebra_withdraw (p, old_select, safi);
  1402. }
  1403. }
  1404. /* Reap old select bgp_info, if it has been removed */
  1405. if (old_select && CHECK_FLAG (old_select->flags, BGP_INFO_REMOVED))
  1406. bgp_info_reap (rn, old_select);
  1407. UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
  1408. return WQ_SUCCESS;
  1409. }
  1410. static void
  1411. bgp_processq_del (struct work_queue *wq, void *data)
  1412. {
  1413. struct bgp_process_queue *pq = data;
  1414. struct bgp_table *table = bgp_node_table (pq->rn);
  1415. bgp_unlock (pq->bgp);
  1416. bgp_unlock_node (pq->rn);
  1417. bgp_table_unlock (table);
  1418. XFREE (MTYPE_BGP_PROCESS_QUEUE, pq);
  1419. }
  1420. static void
  1421. bgp_process_queue_init (void)
  1422. {
  1423. bm->process_main_queue
  1424. = work_queue_new (bm->master, "process_main_queue");
  1425. bm->process_rsclient_queue
  1426. = work_queue_new (bm->master, "process_rsclient_queue");
  1427. if ( !(bm->process_main_queue && bm->process_rsclient_queue) )
  1428. {
  1429. zlog_err ("%s: Failed to allocate work queue", __func__);
  1430. exit (1);
  1431. }
  1432. bm->process_main_queue->spec.workfunc = &bgp_process_main;
  1433. bm->process_main_queue->spec.del_item_data = &bgp_processq_del;
  1434. bm->process_main_queue->spec.max_retries = 0;
  1435. bm->process_main_queue->spec.hold = 50;
  1436. bm->process_rsclient_queue->spec.workfunc = &bgp_process_rsclient;
  1437. bm->process_rsclient_queue->spec.del_item_data = &bgp_processq_del;
  1438. bm->process_rsclient_queue->spec.max_retries = 0;
  1439. bm->process_rsclient_queue->spec.hold = 50;
  1440. }
  1441. void
  1442. bgp_process (struct bgp *bgp, struct bgp_node *rn, afi_t afi, safi_t safi)
  1443. {
  1444. struct bgp_process_queue *pqnode;
  1445. /* already scheduled for processing? */
  1446. if (CHECK_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED))
  1447. return;
  1448. if (rn->info == NULL)
  1449. {
  1450. /* XXX: Perhaps remove before next release, after we've flushed out
  1451. * any obvious cases
  1452. */
  1453. assert (rn->info != NULL);
  1454. char buf[PREFIX_STRLEN];
  1455. zlog_warn ("%s: Called for route_node %s with no routing entries!",
  1456. __func__,
  1457. prefix2str (&(bgp_node_to_rnode (rn)->p), buf, sizeof(buf)));
  1458. return;
  1459. }
  1460. if ( (bm->process_main_queue == NULL) ||
  1461. (bm->process_rsclient_queue == NULL) )
  1462. bgp_process_queue_init ();
  1463. pqnode = XCALLOC (MTYPE_BGP_PROCESS_QUEUE,
  1464. sizeof (struct bgp_process_queue));
  1465. if (!pqnode)
  1466. return;
  1467. /* all unlocked in bgp_processq_del */
  1468. bgp_table_lock (bgp_node_table (rn));
  1469. pqnode->rn = bgp_lock_node (rn);
  1470. pqnode->bgp = bgp;
  1471. bgp_lock (bgp);
  1472. pqnode->afi = afi;
  1473. pqnode->safi = safi;
  1474. switch (bgp_node_table (rn)->type)
  1475. {
  1476. case BGP_TABLE_MAIN:
  1477. work_queue_add (bm->process_main_queue, pqnode);
  1478. break;
  1479. case BGP_TABLE_RSCLIENT:
  1480. work_queue_add (bm->process_rsclient_queue, pqnode);
  1481. break;
  1482. }
  1483. SET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
  1484. return;
  1485. }
  1486. static int
  1487. bgp_maximum_prefix_restart_timer (struct thread *thread)
  1488. {
  1489. struct peer *peer;
  1490. peer = THREAD_ARG (thread);
  1491. peer->t_pmax_restart = NULL;
  1492. if (BGP_DEBUG (events, EVENTS))
  1493. zlog_debug ("%s Maximum-prefix restart timer expired, restore peering",
  1494. peer->host);
  1495. peer_clear (peer);
  1496. return 0;
  1497. }
  1498. int
  1499. bgp_maximum_prefix_overflow (struct peer *peer, afi_t afi,
  1500. safi_t safi, int always)
  1501. {
  1502. if (!CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
  1503. return 0;
  1504. if (peer->pcount[afi][safi] > peer->pmax[afi][safi])
  1505. {
  1506. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT)
  1507. && ! always)
  1508. return 0;
  1509. zlog (peer->log, LOG_INFO,
  1510. "%%MAXPFXEXCEED: No. of %s prefix received from %s %ld exceed, "
  1511. "limit %ld", afi_safi_print (afi, safi), peer->host,
  1512. peer->pcount[afi][safi], peer->pmax[afi][safi]);
  1513. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
  1514. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING))
  1515. return 0;
  1516. {
  1517. u_int8_t ndata[7];
  1518. if (safi == SAFI_MPLS_VPN)
  1519. safi = SAFI_MPLS_LABELED_VPN;
  1520. ndata[0] = (afi >> 8);
  1521. ndata[1] = afi;
  1522. ndata[2] = safi;
  1523. ndata[3] = (peer->pmax[afi][safi] >> 24);
  1524. ndata[4] = (peer->pmax[afi][safi] >> 16);
  1525. ndata[5] = (peer->pmax[afi][safi] >> 8);
  1526. ndata[6] = (peer->pmax[afi][safi]);
  1527. SET_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
  1528. bgp_notify_send_with_data (peer, BGP_NOTIFY_CEASE,
  1529. BGP_NOTIFY_CEASE_MAX_PREFIX, ndata, 7);
  1530. }
  1531. /* restart timer start */
  1532. if (peer->pmax_restart[afi][safi])
  1533. {
  1534. peer->v_pmax_restart = peer->pmax_restart[afi][safi] * 60;
  1535. if (BGP_DEBUG (events, EVENTS))
  1536. zlog_debug ("%s Maximum-prefix restart timer started for %d secs",
  1537. peer->host, peer->v_pmax_restart);
  1538. BGP_TIMER_ON (peer->t_pmax_restart, bgp_maximum_prefix_restart_timer,
  1539. peer->v_pmax_restart);
  1540. }
  1541. return 1;
  1542. }
  1543. else
  1544. UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
  1545. if (peer->pcount[afi][safi] > (peer->pmax[afi][safi] * peer->pmax_threshold[afi][safi] / 100))
  1546. {
  1547. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_THRESHOLD)
  1548. && ! always)
  1549. return 0;
  1550. zlog (peer->log, LOG_INFO,
  1551. "%%MAXPFX: No. of %s prefix received from %s reaches %ld, max %ld",
  1552. afi_safi_print (afi, safi), peer->host, peer->pcount[afi][safi],
  1553. peer->pmax[afi][safi]);
  1554. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_THRESHOLD);
  1555. }
  1556. else
  1557. UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_THRESHOLD);
  1558. return 0;
  1559. }
  1560. /* Unconditionally remove the route from the RIB, without taking
  1561. * damping into consideration (eg, because the session went down)
  1562. */
  1563. static void
  1564. bgp_rib_remove (struct bgp_node *rn, struct bgp_info *ri, struct peer *peer,
  1565. afi_t afi, safi_t safi)
  1566. {
  1567. bgp_aggregate_decrement (peer->bgp, &rn->p, ri, afi, safi);
  1568. if (!CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
  1569. bgp_info_delete (rn, ri); /* keep historical info */
  1570. bgp_process (peer->bgp, rn, afi, safi);
  1571. }
  1572. static void
  1573. bgp_rib_withdraw (struct bgp_node *rn, struct bgp_info *ri, struct peer *peer,
  1574. afi_t afi, safi_t safi, struct prefix_rd *prd)
  1575. {
  1576. int status = BGP_DAMP_NONE;
  1577. /* apply dampening, if result is suppressed, we'll be retaining
  1578. * the bgp_info in the RIB for historical reference.
  1579. */
  1580. if (CHECK_FLAG (peer->bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
  1581. && peer->sort == BGP_PEER_EBGP)
  1582. if ( (status = bgp_damp_withdraw (ri, rn, afi, safi, 0))
  1583. == BGP_DAMP_SUPPRESSED)
  1584. {
  1585. bgp_aggregate_decrement (peer->bgp, &rn->p, ri, afi, safi);
  1586. return;
  1587. }
  1588. bgp_rib_remove (rn, ri, peer, afi, safi);
  1589. }
  1590. static struct bgp_info *
  1591. info_make (int type, int sub_type, struct peer *peer, struct attr *attr,
  1592. struct bgp_node *rn)
  1593. {
  1594. struct bgp_info *new;
  1595. /* Make new BGP info. */
  1596. new = XCALLOC (MTYPE_BGP_ROUTE, sizeof (struct bgp_info));
  1597. new->type = type;
  1598. new->sub_type = sub_type;
  1599. new->peer = peer;
  1600. new->attr = attr;
  1601. new->uptime = bgp_clock ();
  1602. new->net = rn;
  1603. return new;
  1604. }
  1605. static void
  1606. bgp_update_rsclient (struct peer *rsclient, afi_t afi, safi_t safi,
  1607. struct attr *attr, struct peer *peer, struct prefix *p, int type,
  1608. int sub_type, struct prefix_rd *prd, u_char *tag)
  1609. {
  1610. struct bgp_node *rn;
  1611. struct bgp *bgp;
  1612. struct attr new_attr;
  1613. struct attr_extra new_extra;
  1614. struct attr *attr_new;
  1615. struct attr *attr_new2;
  1616. struct bgp_info *ri;
  1617. struct bgp_info *new;
  1618. const char *reason;
  1619. char buf[SU_ADDRSTRLEN];
  1620. /* Do not insert announces from a rsclient into its own 'bgp_table'. */
  1621. if (peer == rsclient)
  1622. return;
  1623. bgp = peer->bgp;
  1624. rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, prd);
  1625. /* Check previously received route. */
  1626. for (ri = rn->info; ri; ri = ri->next)
  1627. if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
  1628. break;
  1629. /* AS path loop check. */
  1630. if (aspath_loop_check (attr->aspath, rsclient->as) > rsclient->allowas_in[afi][safi])
  1631. {
  1632. reason = "as-path contains our own AS;";
  1633. goto filtered;
  1634. }
  1635. /* Route reflector originator ID check. */
  1636. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID)
  1637. && IPV4_ADDR_SAME (&rsclient->remote_id, &attr->extra->originator_id))
  1638. {
  1639. reason = "originator is us;";
  1640. goto filtered;
  1641. }
  1642. new_attr.extra = &new_extra;
  1643. bgp_attr_dup (&new_attr, attr);
  1644. /* Apply export policy. */
  1645. if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT) &&
  1646. bgp_export_modifier (rsclient, peer, p, &new_attr, afi, safi) == RMAP_DENY)
  1647. {
  1648. reason = "export-policy;";
  1649. goto filtered;
  1650. }
  1651. attr_new2 = bgp_attr_intern (&new_attr);
  1652. /* Apply import policy. */
  1653. if (bgp_import_modifier (rsclient, peer, p, &new_attr, afi, safi) == RMAP_DENY)
  1654. {
  1655. bgp_attr_unintern (&attr_new2);
  1656. reason = "import-policy;";
  1657. goto filtered;
  1658. }
  1659. attr_new = bgp_attr_intern (&new_attr);
  1660. bgp_attr_unintern (&attr_new2);
  1661. /* IPv4 unicast next hop check. */
  1662. if ((afi == AFI_IP) && ((safi == SAFI_UNICAST) || safi == SAFI_MULTICAST))
  1663. {
  1664. /* Next hop must not be 0.0.0.0 nor Class D/E address. */
  1665. if (new_attr.nexthop.s_addr == 0
  1666. || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr)))
  1667. {
  1668. bgp_attr_unintern (&attr_new);
  1669. reason = "martian next-hop;";
  1670. goto filtered;
  1671. }
  1672. }
  1673. /* If the update is implicit withdraw. */
  1674. if (ri)
  1675. {
  1676. ri->uptime = bgp_clock ();
  1677. /* Same attribute comes in. */
  1678. if (!CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)
  1679. && attrhash_cmp (ri->attr, attr_new))
  1680. {
  1681. if (BGP_DEBUG (update, UPDATE_IN))
  1682. zlog (peer->log, LOG_DEBUG,
  1683. "%s rcvd %s/%d for RS-client %s...duplicate ignored",
  1684. peer->host,
  1685. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1686. p->prefixlen, rsclient->host);
  1687. bgp_unlock_node (rn);
  1688. bgp_attr_unintern (&attr_new);
  1689. bgp_attr_flush(&new_attr);
  1690. return;
  1691. }
  1692. /* Withdraw/Announce before we fully processed the withdraw */
  1693. if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  1694. bgp_info_restore (rn, ri);
  1695. /* Received Logging. */
  1696. if (BGP_DEBUG (update, UPDATE_IN))
  1697. zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d for RS-client %s",
  1698. peer->host,
  1699. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1700. p->prefixlen, rsclient->host);
  1701. /* The attribute is changed. */
  1702. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  1703. /* Update to new attribute. */
  1704. bgp_attr_unintern (&ri->attr);
  1705. ri->attr = attr_new;
  1706. /* Update MPLS tag. */
  1707. if (safi == SAFI_MPLS_VPN)
  1708. memcpy ((bgp_info_extra_get (ri))->tag, tag, 3);
  1709. bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
  1710. /* Process change. */
  1711. bgp_process (bgp, rn, afi, safi);
  1712. bgp_unlock_node (rn);
  1713. return;
  1714. }
  1715. /* Received Logging. */
  1716. if (BGP_DEBUG (update, UPDATE_IN))
  1717. {
  1718. zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d for RS-client %s",
  1719. peer->host,
  1720. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1721. p->prefixlen, rsclient->host);
  1722. }
  1723. new = info_make(type, sub_type, peer, attr_new, rn);
  1724. /* Update MPLS tag. */
  1725. if (safi == SAFI_MPLS_VPN)
  1726. memcpy ((bgp_info_extra_get (new))->tag, tag, 3);
  1727. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  1728. /* Register new BGP information. */
  1729. bgp_info_add (rn, new);
  1730. /* route_node_get lock */
  1731. bgp_unlock_node (rn);
  1732. /* Process change. */
  1733. bgp_process (bgp, rn, afi, safi);
  1734. return;
  1735. filtered:
  1736. /* This BGP update is filtered. Log the reason then update BGP entry. */
  1737. if (BGP_DEBUG (update, UPDATE_IN))
  1738. zlog (peer->log, LOG_DEBUG,
  1739. "%s rcvd UPDATE about %s/%d -- DENIED for RS-client %s due to: %s",
  1740. peer->host,
  1741. inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1742. p->prefixlen, rsclient->host, reason);
  1743. if (ri)
  1744. bgp_rib_remove (rn, ri, peer, afi, safi);
  1745. bgp_unlock_node (rn);
  1746. return;
  1747. }
  1748. static void
  1749. bgp_withdraw_rsclient (struct peer *rsclient, afi_t afi, safi_t safi,
  1750. struct peer *peer, struct prefix *p, int type, int sub_type,
  1751. struct prefix_rd *prd, u_char *tag)
  1752. {
  1753. struct bgp_node *rn;
  1754. struct bgp_info *ri;
  1755. char buf[SU_ADDRSTRLEN];
  1756. if (rsclient == peer)
  1757. return;
  1758. rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, prd);
  1759. /* Lookup withdrawn route. */
  1760. for (ri = rn->info; ri; ri = ri->next)
  1761. if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
  1762. break;
  1763. /* Withdraw specified route from routing table. */
  1764. if (ri && ! CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
  1765. bgp_rib_withdraw (rn, ri, peer, afi, safi, prd);
  1766. else if (BGP_DEBUG (update, UPDATE_IN))
  1767. zlog (peer->log, LOG_DEBUG,
  1768. "%s Can't find the route %s/%d", peer->host,
  1769. inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1770. p->prefixlen);
  1771. /* Unlock bgp_node_get() lock. */
  1772. bgp_unlock_node (rn);
  1773. }
  1774. static int
  1775. bgp_update_main (struct peer *peer, struct prefix *p, struct attr *attr,
  1776. afi_t afi, safi_t safi, int type, int sub_type,
  1777. struct prefix_rd *prd, u_char *tag, int soft_reconfig)
  1778. {
  1779. int ret;
  1780. int aspath_loop_count = 0;
  1781. struct bgp_node *rn;
  1782. struct bgp *bgp;
  1783. struct attr new_attr;
  1784. struct attr_extra new_extra;
  1785. struct attr *attr_new;
  1786. struct bgp_info *ri;
  1787. struct bgp_info *new;
  1788. const char *reason;
  1789. char buf[SU_ADDRSTRLEN];
  1790. int connected = 0;
  1791. memset (&new_attr, 0, sizeof(struct attr));
  1792. memset (&new_extra, 0, sizeof(struct attr_extra));
  1793. bgp = peer->bgp;
  1794. rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
  1795. /* When peer's soft reconfiguration enabled. Record input packet in
  1796. Adj-RIBs-In. */
  1797. if (! soft_reconfig && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
  1798. && peer != bgp->peer_self)
  1799. bgp_adj_in_set (rn, peer, attr);
  1800. /* Check previously received route. */
  1801. for (ri = rn->info; ri; ri = ri->next)
  1802. if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
  1803. break;
  1804. /* AS path local-as loop check. */
  1805. if (peer->change_local_as)
  1806. {
  1807. if (! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND))
  1808. aspath_loop_count = 1;
  1809. if (aspath_loop_check (attr->aspath, peer->change_local_as) > aspath_loop_count)
  1810. {
  1811. reason = "as-path contains our own AS;";
  1812. goto filtered;
  1813. }
  1814. }
  1815. /* AS path loop check. */
  1816. if (aspath_loop_check (attr->aspath, bgp->as) > peer->allowas_in[afi][safi]
  1817. || (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)
  1818. && aspath_loop_check(attr->aspath, bgp->confed_id)
  1819. > peer->allowas_in[afi][safi]))
  1820. {
  1821. reason = "as-path contains our own AS;";
  1822. goto filtered;
  1823. }
  1824. /* Route reflector originator ID check. */
  1825. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID)
  1826. && IPV4_ADDR_SAME (&bgp->router_id, &attr->extra->originator_id))
  1827. {
  1828. reason = "originator is us;";
  1829. goto filtered;
  1830. }
  1831. /* Route reflector cluster ID check. */
  1832. if (bgp_cluster_filter (peer, attr))
  1833. {
  1834. reason = "reflected from the same cluster;";
  1835. goto filtered;
  1836. }
  1837. /* Apply incoming filter. */
  1838. if (bgp_input_filter (peer, p, attr, afi, safi) == FILTER_DENY)
  1839. {
  1840. reason = "filter;";
  1841. goto filtered;
  1842. }
  1843. new_attr.extra = &new_extra;
  1844. bgp_attr_dup (&new_attr, attr);
  1845. /* Apply incoming route-map.
  1846. * NB: new_attr may now contain newly allocated values from route-map "set"
  1847. * commands, so we need bgp_attr_flush in the error paths, until we intern
  1848. * the attr (which takes over the memory references) */
  1849. if (bgp_input_modifier (peer, p, &new_attr, afi, safi) == RMAP_DENY)
  1850. {
  1851. reason = "route-map;";
  1852. bgp_attr_flush (&new_attr);
  1853. goto filtered;
  1854. }
  1855. /* IPv4 unicast next hop check. */
  1856. if (afi == AFI_IP && safi == SAFI_UNICAST)
  1857. {
  1858. /* Next hop must not be 0.0.0.0 nor Class D/E address. Next hop
  1859. must not be my own address. */
  1860. if (new_attr.nexthop.s_addr == 0
  1861. || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr))
  1862. || bgp_nexthop_self (&new_attr))
  1863. {
  1864. reason = "martian next-hop;";
  1865. bgp_attr_flush (&new_attr);
  1866. goto filtered;
  1867. }
  1868. }
  1869. attr_new = bgp_attr_intern (&new_attr);
  1870. /* If the update is implicit withdraw. */
  1871. if (ri)
  1872. {
  1873. ri->uptime = bgp_clock ();
  1874. /* Same attribute comes in. */
  1875. if (!CHECK_FLAG (ri->flags, BGP_INFO_REMOVED)
  1876. && attrhash_cmp (ri->attr, attr_new))
  1877. {
  1878. if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
  1879. && peer->sort == BGP_PEER_EBGP
  1880. && CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
  1881. {
  1882. if (BGP_DEBUG (update, UPDATE_IN))
  1883. zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d",
  1884. peer->host,
  1885. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1886. p->prefixlen);
  1887. if (bgp_damp_update (ri, rn, afi, safi) != BGP_DAMP_SUPPRESSED)
  1888. {
  1889. bgp_aggregate_increment (bgp, p, ri, afi, safi);
  1890. bgp_process (bgp, rn, afi, safi);
  1891. }
  1892. }
  1893. else /* Duplicate - odd */
  1894. {
  1895. if (BGP_DEBUG (update, UPDATE_IN))
  1896. zlog (peer->log, LOG_DEBUG,
  1897. "%s rcvd %s/%d...duplicate ignored",
  1898. peer->host,
  1899. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1900. p->prefixlen);
  1901. /* graceful restart STALE flag unset. */
  1902. if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
  1903. {
  1904. bgp_info_unset_flag (rn, ri, BGP_INFO_STALE);
  1905. bgp_process (bgp, rn, afi, safi);
  1906. }
  1907. }
  1908. bgp_unlock_node (rn);
  1909. bgp_attr_unintern (&attr_new);
  1910. bgp_attr_flush (&new_attr);
  1911. return 0;
  1912. }
  1913. /* Withdraw/Announce before we fully processed the withdraw */
  1914. if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  1915. {
  1916. if (BGP_DEBUG (update, UPDATE_IN))
  1917. zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d, flapped quicker than processing",
  1918. peer->host,
  1919. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1920. p->prefixlen);
  1921. bgp_info_restore (rn, ri);
  1922. }
  1923. /* Received Logging. */
  1924. if (BGP_DEBUG (update, UPDATE_IN))
  1925. zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d",
  1926. peer->host,
  1927. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  1928. p->prefixlen);
  1929. /* graceful restart STALE flag unset. */
  1930. if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
  1931. bgp_info_unset_flag (rn, ri, BGP_INFO_STALE);
  1932. /* The attribute is changed. */
  1933. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  1934. /* implicit withdraw, decrement aggregate and pcount here.
  1935. * only if update is accepted, they'll increment below.
  1936. */
  1937. bgp_aggregate_decrement (bgp, p, ri, afi, safi);
  1938. /* Update bgp route dampening information. */
  1939. if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
  1940. && peer->sort == BGP_PEER_EBGP)
  1941. {
  1942. /* This is implicit withdraw so we should update dampening
  1943. information. */
  1944. if (! CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
  1945. bgp_damp_withdraw (ri, rn, afi, safi, 1);
  1946. }
  1947. /* Update to new attribute. */
  1948. bgp_attr_unintern (&ri->attr);
  1949. ri->attr = attr_new;
  1950. /* Update MPLS tag. */
  1951. if (safi == SAFI_MPLS_VPN)
  1952. memcpy ((bgp_info_extra_get (ri))->tag, tag, 3);
  1953. bgp_attr_flush (&new_attr);
  1954. /* Update bgp route dampening information. */
  1955. if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
  1956. && peer->sort == BGP_PEER_EBGP)
  1957. {
  1958. /* Now we do normal update dampening. */
  1959. ret = bgp_damp_update (ri, rn, afi, safi);
  1960. if (ret == BGP_DAMP_SUPPRESSED)
  1961. {
  1962. bgp_unlock_node (rn);
  1963. return 0;
  1964. }
  1965. }
  1966. /* Nexthop reachability check. */
  1967. if ((afi == AFI_IP || afi == AFI_IP6) && safi == SAFI_UNICAST)
  1968. {
  1969. if (peer->sort == BGP_PEER_EBGP && peer_ttl (peer) == 1 &&
  1970. ! CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  1971. connected = 1;
  1972. else
  1973. connected = 0;
  1974. if (bgp_find_or_add_nexthop (afi, ri, NULL, connected))
  1975. bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
  1976. else
  1977. {
  1978. if (BGP_DEBUG(nht, NHT))
  1979. {
  1980. char buf1[INET6_ADDRSTRLEN];
  1981. inet_ntop(AF_INET, (const void *)&attr_new->nexthop, buf1, INET6_ADDRSTRLEN);
  1982. zlog_debug("%s(%s): NH unresolved", __FUNCTION__, buf1);
  1983. }
  1984. bgp_info_unset_flag (rn, ri, BGP_INFO_VALID);
  1985. }
  1986. }
  1987. else
  1988. bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
  1989. bgp_attr_flush (&new_attr);
  1990. /* Process change. */
  1991. bgp_aggregate_increment (bgp, p, ri, afi, safi);
  1992. bgp_process (bgp, rn, afi, safi);
  1993. bgp_unlock_node (rn);
  1994. return 0;
  1995. }
  1996. /* Received Logging. */
  1997. if (BGP_DEBUG (update, UPDATE_IN))
  1998. {
  1999. zlog (peer->log, LOG_DEBUG, "%s rcvd %s/%d",
  2000. peer->host,
  2001. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  2002. p->prefixlen);
  2003. }
  2004. /* Make new BGP info. */
  2005. new = info_make(type, sub_type, peer, attr_new, rn);
  2006. /* Update MPLS tag. */
  2007. if (safi == SAFI_MPLS_VPN)
  2008. memcpy ((bgp_info_extra_get (new))->tag, tag, 3);
  2009. /* Nexthop reachability check. */
  2010. if ((afi == AFI_IP || afi == AFI_IP6) && safi == SAFI_UNICAST)
  2011. {
  2012. if (peer->sort == BGP_PEER_EBGP && peer_ttl (peer) == 1 &&
  2013. ! CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  2014. connected = 1;
  2015. else
  2016. connected = 0;
  2017. if (bgp_find_or_add_nexthop (afi, new, NULL, connected))
  2018. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  2019. else
  2020. {
  2021. if (BGP_DEBUG(nht, NHT))
  2022. {
  2023. char buf1[INET6_ADDRSTRLEN];
  2024. inet_ntop(AF_INET, (const void *)&attr_new->nexthop, buf1, INET6_ADDRSTRLEN);
  2025. zlog_debug("%s(%s): NH unresolved", __FUNCTION__, buf1);
  2026. }
  2027. bgp_info_unset_flag (rn, new, BGP_INFO_VALID);
  2028. }
  2029. }
  2030. else
  2031. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  2032. /* Increment prefix */
  2033. bgp_aggregate_increment (bgp, p, new, afi, safi);
  2034. /* Register new BGP information. */
  2035. bgp_info_add (rn, new);
  2036. /* route_node_get lock */
  2037. bgp_unlock_node (rn);
  2038. bgp_attr_flush (&new_attr);
  2039. /* If maximum prefix count is configured and current prefix
  2040. count exeed it. */
  2041. if (bgp_maximum_prefix_overflow (peer, afi, safi, 0))
  2042. return -1;
  2043. /* Process change. */
  2044. bgp_process (bgp, rn, afi, safi);
  2045. return 0;
  2046. /* This BGP update is filtered. Log the reason then update BGP
  2047. entry. */
  2048. filtered:
  2049. if (BGP_DEBUG (update, UPDATE_IN))
  2050. zlog (peer->log, LOG_DEBUG,
  2051. "%s rcvd UPDATE about %s/%d -- DENIED due to: %s",
  2052. peer->host,
  2053. inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  2054. p->prefixlen, reason);
  2055. if (ri)
  2056. bgp_rib_remove (rn, ri, peer, afi, safi);
  2057. bgp_unlock_node (rn);
  2058. bgp_attr_flush (&new_attr);
  2059. return 0;
  2060. }
  2061. int
  2062. bgp_update (struct peer *peer, struct prefix *p, struct attr *attr,
  2063. afi_t afi, safi_t safi, int type, int sub_type,
  2064. struct prefix_rd *prd, u_char *tag, int soft_reconfig)
  2065. {
  2066. struct peer *rsclient;
  2067. struct listnode *node, *nnode;
  2068. struct bgp *bgp;
  2069. int ret;
  2070. ret = bgp_update_main (peer, p, attr, afi, safi, type, sub_type, prd, tag,
  2071. soft_reconfig);
  2072. bgp = peer->bgp;
  2073. /* Process the update for each RS-client. */
  2074. for (ALL_LIST_ELEMENTS (bgp->rsclient, node, nnode, rsclient))
  2075. {
  2076. if (CHECK_FLAG (rsclient->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  2077. bgp_update_rsclient (rsclient, afi, safi, attr, peer, p, type,
  2078. sub_type, prd, tag);
  2079. }
  2080. return ret;
  2081. }
  2082. int
  2083. bgp_withdraw (struct peer *peer, struct prefix *p, struct attr *attr,
  2084. afi_t afi, safi_t safi, int type, int sub_type,
  2085. struct prefix_rd *prd, u_char *tag)
  2086. {
  2087. struct bgp *bgp;
  2088. char buf[SU_ADDRSTRLEN];
  2089. struct bgp_node *rn;
  2090. struct bgp_info *ri;
  2091. struct peer *rsclient;
  2092. struct listnode *node, *nnode;
  2093. bgp = peer->bgp;
  2094. /* Lookup node. */
  2095. rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
  2096. /* Cisco IOS 12.4(24)T4 on session establishment sends withdraws for all
  2097. * routes that are filtered. This tanks out Quagga RS pretty badly due to
  2098. * the iteration over all RS clients.
  2099. * Since we need to remove the entry from adj_in anyway, do that first and
  2100. * if there was no entry, we don't need to do anything more. */
  2101. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
  2102. && peer != bgp->peer_self)
  2103. if (!bgp_adj_in_unset (rn, peer))
  2104. {
  2105. if (BGP_DEBUG (update, UPDATE_IN))
  2106. zlog (peer->log, LOG_DEBUG, "%s withdrawing route %s/%d "
  2107. "not in adj-in", peer->host,
  2108. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  2109. p->prefixlen);
  2110. bgp_unlock_node (rn);
  2111. return 0;
  2112. }
  2113. /* Process the withdraw for each RS-client. */
  2114. for (ALL_LIST_ELEMENTS (bgp->rsclient, node, nnode, rsclient))
  2115. {
  2116. if (CHECK_FLAG (rsclient->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  2117. bgp_withdraw_rsclient (rsclient, afi, safi, peer, p, type, sub_type, prd, tag);
  2118. }
  2119. /* Logging. */
  2120. if (BGP_DEBUG (update, UPDATE_IN))
  2121. zlog (peer->log, LOG_DEBUG, "%s rcvd UPDATE about %s/%d -- withdrawn",
  2122. peer->host,
  2123. inet_ntop(p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  2124. p->prefixlen);
  2125. /* Lookup withdrawn route. */
  2126. for (ri = rn->info; ri; ri = ri->next)
  2127. if (ri->peer == peer && ri->type == type && ri->sub_type == sub_type)
  2128. break;
  2129. /* Withdraw specified route from routing table. */
  2130. if (ri && ! CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
  2131. bgp_rib_withdraw (rn, ri, peer, afi, safi, prd);
  2132. else if (BGP_DEBUG (update, UPDATE_IN))
  2133. zlog (peer->log, LOG_DEBUG,
  2134. "%s Can't find the route %s/%d", peer->host,
  2135. inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  2136. p->prefixlen);
  2137. /* Unlock bgp_node_get() lock. */
  2138. bgp_unlock_node (rn);
  2139. return 0;
  2140. }
  2141. void
  2142. bgp_default_originate (struct peer *peer, afi_t afi, safi_t safi, int withdraw)
  2143. {
  2144. struct bgp *bgp;
  2145. struct attr attr;
  2146. struct aspath *aspath;
  2147. struct prefix p;
  2148. struct peer *from;
  2149. struct bgp_node *rn;
  2150. struct bgp_info *ri;
  2151. int ret = RMAP_DENYMATCH;
  2152. if (!(afi == AFI_IP || afi == AFI_IP6))
  2153. return;
  2154. bgp = peer->bgp;
  2155. from = bgp->peer_self;
  2156. bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
  2157. aspath = attr.aspath;
  2158. attr.local_pref = bgp->default_local_pref;
  2159. memcpy (&attr.nexthop, &peer->nexthop.v4, IPV4_MAX_BYTELEN);
  2160. if (afi == AFI_IP)
  2161. str2prefix ("0.0.0.0/0", &p);
  2162. else if (afi == AFI_IP6)
  2163. {
  2164. struct attr_extra *ae = attr.extra;
  2165. str2prefix ("::/0", &p);
  2166. /* IPv6 global nexthop must be included. */
  2167. memcpy (&ae->mp_nexthop_global, &peer->nexthop.v6_global,
  2168. IPV6_MAX_BYTELEN);
  2169. ae->mp_nexthop_len = 16;
  2170. /* If the peer is on shared nextwork and we have link-local
  2171. nexthop set it. */
  2172. if (peer->shared_network
  2173. && !IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_local))
  2174. {
  2175. memcpy (&ae->mp_nexthop_local, &peer->nexthop.v6_local,
  2176. IPV6_MAX_BYTELEN);
  2177. ae->mp_nexthop_len = 32;
  2178. }
  2179. }
  2180. if (peer->default_rmap[afi][safi].name)
  2181. {
  2182. SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
  2183. for (rn = bgp_table_top(bgp->rib[afi][safi]); rn; rn = bgp_route_next(rn))
  2184. {
  2185. for (ri = rn->info; ri; ri = ri->next)
  2186. {
  2187. struct attr dummy_attr;
  2188. struct attr_extra dummy_extra;
  2189. struct bgp_info info;
  2190. /* Provide dummy so the route-map can't modify the attributes */
  2191. dummy_attr.extra = &dummy_extra;
  2192. bgp_attr_dup(&dummy_attr, ri->attr);
  2193. info.peer = ri->peer;
  2194. info.attr = &dummy_attr;
  2195. ret = route_map_apply(peer->default_rmap[afi][safi].map, &rn->p,
  2196. RMAP_BGP, &info);
  2197. /* The route map might have set attributes. If we don't flush them
  2198. * here, they will be leaked. */
  2199. bgp_attr_flush(&dummy_attr);
  2200. if (ret != RMAP_DENYMATCH)
  2201. break;
  2202. }
  2203. if (ret != RMAP_DENYMATCH)
  2204. break;
  2205. }
  2206. bgp->peer_self->rmap_type = 0;
  2207. if (ret == RMAP_DENYMATCH)
  2208. withdraw = 1;
  2209. }
  2210. if (withdraw)
  2211. {
  2212. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE))
  2213. bgp_default_withdraw_send (peer, afi, safi);
  2214. UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE);
  2215. }
  2216. else
  2217. {
  2218. if (! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE))
  2219. {
  2220. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_DEFAULT_ORIGINATE);
  2221. bgp_default_update_send (peer, &attr, afi, safi, from);
  2222. }
  2223. }
  2224. bgp_attr_extra_free (&attr);
  2225. aspath_unintern (&aspath);
  2226. }
  2227. static void
  2228. bgp_announce_table (struct peer *peer, afi_t afi, safi_t safi,
  2229. struct bgp_table *table, int rsclient)
  2230. {
  2231. struct bgp_node *rn;
  2232. struct bgp_info *ri;
  2233. struct attr attr;
  2234. struct attr_extra extra;
  2235. memset(&extra, 0, sizeof(extra));
  2236. if (! table)
  2237. table = (rsclient) ? peer->rib[afi][safi] : peer->bgp->rib[afi][safi];
  2238. if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP)
  2239. && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE))
  2240. bgp_default_originate (peer, afi, safi, 0);
  2241. /* It's initialized in bgp_announce_[check|check_rsclient]() */
  2242. attr.extra = &extra;
  2243. for (rn = bgp_table_top (table); rn; rn = bgp_route_next(rn))
  2244. for (ri = rn->info; ri; ri = ri->next)
  2245. if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED) && ri->peer != peer)
  2246. {
  2247. if ( (rsclient) ?
  2248. (bgp_announce_check_rsclient (ri, peer, &rn->p, &attr, afi, safi))
  2249. : (bgp_announce_check (ri, peer, &rn->p, &attr, afi, safi)))
  2250. bgp_adj_out_set (rn, peer, &rn->p, &attr, afi, safi, ri);
  2251. else
  2252. bgp_adj_out_unset (rn, peer, &rn->p, afi, safi);
  2253. }
  2254. bgp_attr_flush_encap(&attr);
  2255. }
  2256. void
  2257. bgp_announce_route (struct peer *peer, afi_t afi, safi_t safi)
  2258. {
  2259. struct bgp_node *rn;
  2260. struct bgp_table *table;
  2261. if (peer->status != Established)
  2262. return;
  2263. if (! peer->afc_nego[afi][safi])
  2264. return;
  2265. /* First update is deferred until ORF or ROUTE-REFRESH is received */
  2266. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
  2267. return;
  2268. if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
  2269. bgp_announce_table (peer, afi, safi, NULL, 0);
  2270. else
  2271. for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
  2272. rn = bgp_route_next(rn))
  2273. if ((table = (rn->info)) != NULL)
  2274. bgp_announce_table (peer, afi, safi, table, 0);
  2275. if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  2276. bgp_announce_table (peer, afi, safi, NULL, 1);
  2277. }
  2278. void
  2279. bgp_announce_route_all (struct peer *peer)
  2280. {
  2281. afi_t afi;
  2282. safi_t safi;
  2283. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  2284. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  2285. bgp_announce_route (peer, afi, safi);
  2286. }
  2287. static void
  2288. bgp_soft_reconfig_table_rsclient (struct peer *rsclient, afi_t afi,
  2289. safi_t safi, struct bgp_table *table, struct prefix_rd *prd)
  2290. {
  2291. struct bgp_node *rn;
  2292. struct bgp_adj_in *ain;
  2293. if (! table)
  2294. table = rsclient->bgp->rib[afi][safi];
  2295. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  2296. for (ain = rn->adj_in; ain; ain = ain->next)
  2297. {
  2298. struct bgp_info *ri = rn->info;
  2299. u_char *tag = (ri && ri->extra) ? ri->extra->tag : NULL;
  2300. bgp_update_rsclient (rsclient, afi, safi, ain->attr, ain->peer,
  2301. &rn->p, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, prd, tag);
  2302. }
  2303. }
  2304. void
  2305. bgp_soft_reconfig_rsclient (struct peer *rsclient, afi_t afi, safi_t safi)
  2306. {
  2307. struct bgp_table *table;
  2308. struct bgp_node *rn;
  2309. if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
  2310. bgp_soft_reconfig_table_rsclient (rsclient, afi, safi, NULL, NULL);
  2311. else
  2312. for (rn = bgp_table_top (rsclient->bgp->rib[afi][safi]); rn;
  2313. rn = bgp_route_next (rn))
  2314. if ((table = rn->info) != NULL)
  2315. {
  2316. struct prefix_rd prd;
  2317. prd.family = AF_UNSPEC;
  2318. prd.prefixlen = 64;
  2319. memcpy(&prd.val, rn->p.u.val, 8);
  2320. bgp_soft_reconfig_table_rsclient (rsclient, afi, safi, table, &prd);
  2321. }
  2322. }
  2323. static void
  2324. bgp_soft_reconfig_table (struct peer *peer, afi_t afi, safi_t safi,
  2325. struct bgp_table *table, struct prefix_rd *prd)
  2326. {
  2327. int ret;
  2328. struct bgp_node *rn;
  2329. struct bgp_adj_in *ain;
  2330. if (! table)
  2331. table = peer->bgp->rib[afi][safi];
  2332. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  2333. for (ain = rn->adj_in; ain; ain = ain->next)
  2334. {
  2335. if (ain->peer == peer)
  2336. {
  2337. struct bgp_info *ri = rn->info;
  2338. u_char *tag = (ri && ri->extra) ? ri->extra->tag : NULL;
  2339. ret = bgp_update (peer, &rn->p, ain->attr, afi, safi,
  2340. ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
  2341. prd, tag, 1);
  2342. if (ret < 0)
  2343. {
  2344. bgp_unlock_node (rn);
  2345. return;
  2346. }
  2347. continue;
  2348. }
  2349. }
  2350. }
  2351. void
  2352. bgp_soft_reconfig_in (struct peer *peer, afi_t afi, safi_t safi)
  2353. {
  2354. struct bgp_node *rn;
  2355. struct bgp_table *table;
  2356. if (peer->status != Established)
  2357. return;
  2358. if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
  2359. bgp_soft_reconfig_table (peer, afi, safi, NULL, NULL);
  2360. else
  2361. for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
  2362. rn = bgp_route_next (rn))
  2363. if ((table = rn->info) != NULL)
  2364. {
  2365. struct prefix_rd prd;
  2366. prd.family = AF_UNSPEC;
  2367. prd.prefixlen = 64;
  2368. memcpy(&prd.val, rn->p.u.val, 8);
  2369. bgp_soft_reconfig_table (peer, afi, safi, table, &prd);
  2370. }
  2371. }
  2372. struct bgp_clear_node_queue
  2373. {
  2374. struct bgp_node *rn;
  2375. enum bgp_clear_route_type purpose;
  2376. };
  2377. static wq_item_status
  2378. bgp_clear_route_node (struct work_queue *wq, void *data)
  2379. {
  2380. struct bgp_clear_node_queue *cnq = data;
  2381. struct bgp_node *rn = cnq->rn;
  2382. struct peer *peer = wq->spec.data;
  2383. struct bgp_info *ri;
  2384. afi_t afi = bgp_node_table (rn)->afi;
  2385. safi_t safi = bgp_node_table (rn)->safi;
  2386. assert (rn && peer);
  2387. for (ri = rn->info; ri; ri = ri->next)
  2388. if (ri->peer == peer || cnq->purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
  2389. {
  2390. /* graceful restart STALE flag set. */
  2391. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)
  2392. && peer->nsf[afi][safi]
  2393. && ! CHECK_FLAG (ri->flags, BGP_INFO_STALE)
  2394. && ! CHECK_FLAG (ri->flags, BGP_INFO_UNUSEABLE))
  2395. bgp_info_set_flag (rn, ri, BGP_INFO_STALE);
  2396. else
  2397. bgp_rib_remove (rn, ri, peer, afi, safi);
  2398. break;
  2399. }
  2400. return WQ_SUCCESS;
  2401. }
  2402. static void
  2403. bgp_clear_node_queue_del (struct work_queue *wq, void *data)
  2404. {
  2405. struct bgp_clear_node_queue *cnq = data;
  2406. struct bgp_node *rn = cnq->rn;
  2407. struct bgp_table *table = bgp_node_table (rn);
  2408. bgp_unlock_node (rn);
  2409. bgp_table_unlock (table);
  2410. XFREE (MTYPE_BGP_CLEAR_NODE_QUEUE, cnq);
  2411. }
  2412. static void
  2413. bgp_clear_node_complete (struct work_queue *wq)
  2414. {
  2415. struct peer *peer = wq->spec.data;
  2416. /* Tickle FSM to start moving again */
  2417. BGP_EVENT_ADD (peer, Clearing_Completed);
  2418. peer_unlock (peer); /* bgp_clear_route */
  2419. }
  2420. static void
  2421. bgp_clear_node_queue_init (struct peer *peer)
  2422. {
  2423. char wname[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
  2424. snprintf (wname, sizeof(wname), "clear %s", peer->host);
  2425. #undef CLEAR_QUEUE_NAME_LEN
  2426. if ( (peer->clear_node_queue = work_queue_new (bm->master, wname)) == NULL)
  2427. {
  2428. zlog_err ("%s: Failed to allocate work queue", __func__);
  2429. exit (1);
  2430. }
  2431. peer->clear_node_queue->spec.hold = 10;
  2432. peer->clear_node_queue->spec.workfunc = &bgp_clear_route_node;
  2433. peer->clear_node_queue->spec.del_item_data = &bgp_clear_node_queue_del;
  2434. peer->clear_node_queue->spec.completion_func = &bgp_clear_node_complete;
  2435. peer->clear_node_queue->spec.max_retries = 0;
  2436. /* we only 'lock' this peer reference when the queue is actually active */
  2437. peer->clear_node_queue->spec.data = peer;
  2438. }
  2439. static void
  2440. bgp_clear_route_table (struct peer *peer, afi_t afi, safi_t safi,
  2441. struct bgp_table *table, struct peer *rsclient,
  2442. enum bgp_clear_route_type purpose)
  2443. {
  2444. struct bgp_node *rn;
  2445. if (! table)
  2446. table = (rsclient) ? rsclient->rib[afi][safi] : peer->bgp->rib[afi][safi];
  2447. /* If still no table => afi/safi isn't configured at all or smth. */
  2448. if (! table)
  2449. return;
  2450. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  2451. {
  2452. struct bgp_info *ri;
  2453. struct bgp_adj_in *ain;
  2454. struct bgp_adj_out *aout;
  2455. /* XXX:TODO: This is suboptimal, every non-empty route_node is
  2456. * queued for every clearing peer, regardless of whether it is
  2457. * relevant to the peer at hand.
  2458. *
  2459. * Overview: There are 3 different indices which need to be
  2460. * scrubbed, potentially, when a peer is removed:
  2461. *
  2462. * 1 peer's routes visible via the RIB (ie accepted routes)
  2463. * 2 peer's routes visible by the (optional) peer's adj-in index
  2464. * 3 other routes visible by the peer's adj-out index
  2465. *
  2466. * 3 there is no hurry in scrubbing, once the struct peer is
  2467. * removed from bgp->peer, we could just GC such deleted peer's
  2468. * adj-outs at our leisure.
  2469. *
  2470. * 1 and 2 must be 'scrubbed' in some way, at least made
  2471. * invisible via RIB index before peer session is allowed to be
  2472. * brought back up. So one needs to know when such a 'search' is
  2473. * complete.
  2474. *
  2475. * Ideally:
  2476. *
  2477. * - there'd be a single global queue or a single RIB walker
  2478. * - rather than tracking which route_nodes still need to be
  2479. * examined on a peer basis, we'd track which peers still
  2480. * aren't cleared
  2481. *
  2482. * Given that our per-peer prefix-counts now should be reliable,
  2483. * this may actually be achievable. It doesn't seem to be a huge
  2484. * problem at this time,
  2485. */
  2486. for (ain = rn->adj_in; ain; ain = ain->next)
  2487. if (ain->peer == peer || purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
  2488. {
  2489. bgp_adj_in_remove (rn, ain);
  2490. bgp_unlock_node (rn);
  2491. break;
  2492. }
  2493. for (aout = rn->adj_out; aout; aout = aout->next)
  2494. if (aout->peer == peer || purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
  2495. {
  2496. bgp_adj_out_remove (rn, aout, peer, afi, safi);
  2497. bgp_unlock_node (rn);
  2498. break;
  2499. }
  2500. for (ri = rn->info; ri; ri = ri->next)
  2501. if (ri->peer == peer || purpose == BGP_CLEAR_ROUTE_MY_RSCLIENT)
  2502. {
  2503. struct bgp_clear_node_queue *cnq;
  2504. /* both unlocked in bgp_clear_node_queue_del */
  2505. bgp_table_lock (bgp_node_table (rn));
  2506. bgp_lock_node (rn);
  2507. cnq = XCALLOC (MTYPE_BGP_CLEAR_NODE_QUEUE,
  2508. sizeof (struct bgp_clear_node_queue));
  2509. cnq->rn = rn;
  2510. cnq->purpose = purpose;
  2511. work_queue_add (peer->clear_node_queue, cnq);
  2512. break;
  2513. }
  2514. }
  2515. return;
  2516. }
  2517. void
  2518. bgp_clear_route (struct peer *peer, afi_t afi, safi_t safi,
  2519. enum bgp_clear_route_type purpose)
  2520. {
  2521. struct bgp_node *rn;
  2522. struct bgp_table *table;
  2523. struct peer *rsclient;
  2524. struct listnode *node, *nnode;
  2525. if (peer->clear_node_queue == NULL)
  2526. bgp_clear_node_queue_init (peer);
  2527. /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
  2528. * Idle until it receives a Clearing_Completed event. This protects
  2529. * against peers which flap faster than we can we clear, which could
  2530. * lead to:
  2531. *
  2532. * a) race with routes from the new session being installed before
  2533. * clear_route_node visits the node (to delete the route of that
  2534. * peer)
  2535. * b) resource exhaustion, clear_route_node likely leads to an entry
  2536. * on the process_main queue. Fast-flapping could cause that queue
  2537. * to grow and grow.
  2538. */
  2539. /* lock peer in assumption that clear-node-queue will get nodes; if so,
  2540. * the unlock will happen upon work-queue completion; other wise, the
  2541. * unlock happens at the end of this function.
  2542. */
  2543. if (!peer->clear_node_queue->thread)
  2544. peer_lock (peer); /* bgp_clear_node_complete */
  2545. switch (purpose)
  2546. {
  2547. case BGP_CLEAR_ROUTE_NORMAL:
  2548. if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
  2549. bgp_clear_route_table (peer, afi, safi, NULL, NULL, purpose);
  2550. else
  2551. for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
  2552. rn = bgp_route_next (rn))
  2553. if ((table = rn->info) != NULL)
  2554. bgp_clear_route_table (peer, afi, safi, table, NULL, purpose);
  2555. for (ALL_LIST_ELEMENTS (peer->bgp->rsclient, node, nnode, rsclient))
  2556. if (CHECK_FLAG(rsclient->af_flags[afi][safi],
  2557. PEER_FLAG_RSERVER_CLIENT))
  2558. bgp_clear_route_table (peer, afi, safi, NULL, rsclient, purpose);
  2559. break;
  2560. case BGP_CLEAR_ROUTE_MY_RSCLIENT:
  2561. /*
  2562. * gpz 091009: TBD why don't we have special handling for
  2563. * SAFI_MPLS_VPN here in the original quagga code?
  2564. * (and, by extension, for SAFI_ENCAP)
  2565. */
  2566. bgp_clear_route_table (peer, afi, safi, NULL, peer, purpose);
  2567. break;
  2568. default:
  2569. assert (0);
  2570. break;
  2571. }
  2572. /* unlock if no nodes got added to the clear-node-queue. */
  2573. if (!peer->clear_node_queue->thread)
  2574. peer_unlock (peer);
  2575. }
  2576. void
  2577. bgp_clear_route_all (struct peer *peer)
  2578. {
  2579. afi_t afi;
  2580. safi_t safi;
  2581. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  2582. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  2583. bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
  2584. }
  2585. /*
  2586. * Finish freeing things when exiting
  2587. */
  2588. static void
  2589. bgp_drain_workqueue_immediate (struct work_queue *wq)
  2590. {
  2591. if (!wq)
  2592. return;
  2593. if (!wq->thread)
  2594. {
  2595. /*
  2596. * no thread implies no queued items
  2597. */
  2598. assert(!wq->items->count);
  2599. return;
  2600. }
  2601. while (wq->items->count)
  2602. {
  2603. if (wq->thread)
  2604. thread_cancel(wq->thread);
  2605. work_queue_run(wq->thread);
  2606. }
  2607. }
  2608. /*
  2609. * Special function to process clear node queue when bgpd is exiting
  2610. * and the thread scheduler is no longer running.
  2611. */
  2612. void
  2613. bgp_peer_clear_node_queue_drain_immediate(struct peer *peer)
  2614. {
  2615. if (!peer)
  2616. return;
  2617. bgp_drain_workqueue_immediate(peer->clear_node_queue);
  2618. }
  2619. /*
  2620. * The work queues are not specific to a BGP instance, but the
  2621. * items in them refer to BGP instances, so this should be called
  2622. * before each BGP instance is deleted.
  2623. */
  2624. void
  2625. bgp_process_queues_drain_immediate(void)
  2626. {
  2627. bgp_drain_workqueue_immediate(bm->process_main_queue);
  2628. bgp_drain_workqueue_immediate(bm->process_rsclient_queue);
  2629. }
  2630. void
  2631. bgp_clear_adj_in (struct peer *peer, afi_t afi, safi_t safi)
  2632. {
  2633. struct bgp_table *table;
  2634. struct bgp_node *rn;
  2635. struct bgp_adj_in *ain;
  2636. table = peer->bgp->rib[afi][safi];
  2637. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  2638. for (ain = rn->adj_in; ain ; ain = ain->next)
  2639. if (ain->peer == peer)
  2640. {
  2641. bgp_adj_in_remove (rn, ain);
  2642. bgp_unlock_node (rn);
  2643. break;
  2644. }
  2645. }
  2646. void
  2647. bgp_clear_stale_route (struct peer *peer, afi_t afi, safi_t safi)
  2648. {
  2649. struct bgp_node *rn;
  2650. struct bgp_info *ri;
  2651. struct bgp_table *table;
  2652. table = peer->bgp->rib[afi][safi];
  2653. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  2654. {
  2655. for (ri = rn->info; ri; ri = ri->next)
  2656. if (ri->peer == peer)
  2657. {
  2658. if (CHECK_FLAG (ri->flags, BGP_INFO_STALE))
  2659. bgp_rib_remove (rn, ri, peer, afi, safi);
  2660. break;
  2661. }
  2662. }
  2663. }
  2664. static void
  2665. bgp_cleanup_table(struct bgp_table *table, safi_t safi)
  2666. {
  2667. struct bgp_node *rn;
  2668. struct bgp_info *ri;
  2669. struct bgp_info *next;
  2670. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  2671. for (ri = rn->info; ri; ri = next)
  2672. {
  2673. next = ri->next;
  2674. if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
  2675. && ri->type == ZEBRA_ROUTE_BGP
  2676. && ri->sub_type == BGP_ROUTE_NORMAL)
  2677. bgp_zebra_withdraw (&rn->p, ri, safi);
  2678. }
  2679. }
  2680. /* Delete all kernel routes. */
  2681. void
  2682. bgp_cleanup_routes (void)
  2683. {
  2684. struct bgp *bgp;
  2685. struct listnode *node, *nnode;
  2686. afi_t afi;
  2687. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  2688. {
  2689. for (afi = AFI_IP; afi < AFI_MAX; ++afi)
  2690. {
  2691. struct bgp_node *rn;
  2692. bgp_cleanup_table(bgp->rib[afi][SAFI_UNICAST], SAFI_UNICAST);
  2693. /*
  2694. * VPN and ENCAP tables are two-level (RD is top level)
  2695. */
  2696. for (rn = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); rn;
  2697. rn = bgp_route_next (rn))
  2698. {
  2699. if (rn->info)
  2700. {
  2701. bgp_cleanup_table((struct bgp_table *)(rn->info), SAFI_MPLS_VPN);
  2702. bgp_table_finish ((struct bgp_table **)&(rn->info));
  2703. rn->info = NULL;
  2704. bgp_unlock_node(rn);
  2705. }
  2706. }
  2707. for (rn = bgp_table_top(bgp->rib[afi][SAFI_ENCAP]); rn;
  2708. rn = bgp_route_next (rn))
  2709. {
  2710. if (rn->info)
  2711. {
  2712. bgp_cleanup_table((struct bgp_table *)(rn->info), SAFI_ENCAP);
  2713. bgp_table_finish ((struct bgp_table **)&(rn->info));
  2714. rn->info = NULL;
  2715. bgp_unlock_node(rn);
  2716. }
  2717. }
  2718. }
  2719. }
  2720. }
  2721. void
  2722. bgp_reset (void)
  2723. {
  2724. vty_reset ();
  2725. bgp_zclient_reset ();
  2726. access_list_reset ();
  2727. prefix_list_reset ();
  2728. }
  2729. /* Parse NLRI stream. Withdraw NLRI is recognized by NULL attr
  2730. value. */
  2731. int
  2732. bgp_nlri_parse_ip (struct peer *peer, struct attr *attr,
  2733. struct bgp_nlri *packet)
  2734. {
  2735. u_char *pnt;
  2736. u_char *lim;
  2737. struct prefix p;
  2738. int psize;
  2739. int ret;
  2740. /* Check peer status. */
  2741. if (peer->status != Established)
  2742. return 0;
  2743. pnt = packet->nlri;
  2744. lim = pnt + packet->length;
  2745. /* RFC4771 6.3 The NLRI field in the UPDATE message is checked for
  2746. syntactic validity. If the field is syntactically incorrect,
  2747. then the Error Subcode is set to Invalid Network Field. */
  2748. for (; pnt < lim; pnt += psize)
  2749. {
  2750. /* Clear prefix structure. */
  2751. memset (&p, 0, sizeof (struct prefix));
  2752. /* Fetch prefix length. */
  2753. p.prefixlen = *pnt++;
  2754. /* afi/safi validity already verified by caller, bgp_update_receive */
  2755. p.family = afi2family (packet->afi);
  2756. /* Prefix length check. */
  2757. if (p.prefixlen > prefix_blen (&p) * 8)
  2758. {
  2759. plog_err (peer->log,
  2760. "%s [Error] Update packet error"
  2761. " (wrong prefix length %u for afi %u)",
  2762. peer->host, p.prefixlen, packet->afi);
  2763. return -1;
  2764. }
  2765. /* Packet size overflow check. */
  2766. psize = PSIZE (p.prefixlen);
  2767. /* When packet overflow occur return immediately. */
  2768. if (pnt + psize > lim)
  2769. {
  2770. plog_err (peer->log,
  2771. "%s [Error] Update packet error"
  2772. " (prefix length %u overflows packet)",
  2773. peer->host, p.prefixlen);
  2774. return -1;
  2775. }
  2776. /* Defensive coding, double-check the psize fits in a struct prefix */
  2777. if (psize > (ssize_t) sizeof(p.u))
  2778. {
  2779. plog_err (peer->log,
  2780. "%s [Error] Update packet error"
  2781. " (prefix length %u too large for prefix storage %zu!?!!",
  2782. peer->host, p.prefixlen, sizeof(p.u));
  2783. return -1;
  2784. }
  2785. /* Fetch prefix from NLRI packet. */
  2786. memcpy (&p.u.prefix, pnt, psize);
  2787. /* Check address. */
  2788. if (packet->afi == AFI_IP && packet->safi == SAFI_UNICAST)
  2789. {
  2790. if (IN_CLASSD (ntohl (p.u.prefix4.s_addr)))
  2791. {
  2792. /*
  2793. * From RFC4271 Section 6.3:
  2794. *
  2795. * If a prefix in the NLRI field is semantically incorrect
  2796. * (e.g., an unexpected multicast IP address), an error SHOULD
  2797. * be logged locally, and the prefix SHOULD be ignored.
  2798. */
  2799. zlog (peer->log, LOG_ERR,
  2800. "%s: IPv4 unicast NLRI is multicast address %s, ignoring",
  2801. peer->host, inet_ntoa (p.u.prefix4));
  2802. continue;
  2803. }
  2804. }
  2805. /* Check address. */
  2806. if (packet->afi == AFI_IP6 && packet->safi == SAFI_UNICAST)
  2807. {
  2808. if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
  2809. {
  2810. char buf[BUFSIZ];
  2811. zlog (peer->log, LOG_ERR,
  2812. "%s: IPv6 unicast NLRI is link-local address %s, ignoring",
  2813. peer->host,
  2814. inet_ntop (AF_INET6, &p.u.prefix6, buf, BUFSIZ));
  2815. continue;
  2816. }
  2817. if (IN6_IS_ADDR_MULTICAST (&p.u.prefix6))
  2818. {
  2819. char buf[BUFSIZ];
  2820. zlog (peer->log, LOG_ERR,
  2821. "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
  2822. peer->host,
  2823. inet_ntop (AF_INET6, &p.u.prefix6, buf, BUFSIZ));
  2824. continue;
  2825. }
  2826. }
  2827. /* Normal process. */
  2828. if (attr)
  2829. ret = bgp_update (peer, &p, attr, packet->afi, packet->safi,
  2830. ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, NULL, 0);
  2831. else
  2832. ret = bgp_withdraw (peer, &p, attr, packet->afi, packet->safi,
  2833. ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, NULL);
  2834. /* Address family configuration mismatch or maximum-prefix count
  2835. overflow. */
  2836. if (ret < 0)
  2837. return -1;
  2838. }
  2839. /* Packet length consistency check. */
  2840. if (pnt != lim)
  2841. {
  2842. plog_err (peer->log,
  2843. "%s [Error] Update packet error"
  2844. " (prefix length mismatch with total length)",
  2845. peer->host);
  2846. return -1;
  2847. }
  2848. return 0;
  2849. }
  2850. static struct bgp_static *
  2851. bgp_static_new (void)
  2852. {
  2853. return XCALLOC (MTYPE_BGP_STATIC, sizeof (struct bgp_static));
  2854. }
  2855. static void
  2856. bgp_static_free (struct bgp_static *bgp_static)
  2857. {
  2858. if (bgp_static->rmap.name)
  2859. free (bgp_static->rmap.name);
  2860. XFREE (MTYPE_BGP_STATIC, bgp_static);
  2861. }
  2862. static void
  2863. bgp_static_withdraw_rsclient (struct bgp *bgp, struct peer *rsclient,
  2864. struct prefix *p, afi_t afi, safi_t safi)
  2865. {
  2866. struct bgp_node *rn;
  2867. struct bgp_info *ri;
  2868. rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, NULL);
  2869. /* Check selected route and self inserted route. */
  2870. for (ri = rn->info; ri; ri = ri->next)
  2871. if (ri->peer == bgp->peer_self
  2872. && ri->type == ZEBRA_ROUTE_BGP
  2873. && ri->sub_type == BGP_ROUTE_STATIC)
  2874. break;
  2875. /* Withdraw static BGP route from routing table. */
  2876. if (ri)
  2877. {
  2878. bgp_info_delete (rn, ri);
  2879. bgp_process (bgp, rn, afi, safi);
  2880. }
  2881. /* Unlock bgp_node_lookup. */
  2882. bgp_unlock_node (rn);
  2883. }
  2884. static void
  2885. bgp_static_update_rsclient (struct peer *rsclient, struct prefix *p,
  2886. struct bgp_static *bgp_static,
  2887. afi_t afi, safi_t safi)
  2888. {
  2889. struct bgp_node *rn;
  2890. struct bgp_info *ri;
  2891. struct bgp_info *new;
  2892. struct bgp_info info;
  2893. struct attr *attr_new;
  2894. struct attr attr;
  2895. struct attr new_attr;
  2896. struct attr_extra new_extra;
  2897. struct bgp *bgp;
  2898. int ret;
  2899. char buf[SU_ADDRSTRLEN];
  2900. bgp = rsclient->bgp;
  2901. assert (bgp_static);
  2902. if (!bgp_static)
  2903. return;
  2904. rn = bgp_afi_node_get (rsclient->rib[afi][safi], afi, safi, p, NULL);
  2905. bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
  2906. attr.nexthop = bgp_static->igpnexthop;
  2907. attr.med = bgp_static->igpmetric;
  2908. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
  2909. if (bgp_static->atomic)
  2910. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
  2911. /* Apply network route-map for export to this rsclient. */
  2912. if (bgp_static->rmap.name)
  2913. {
  2914. struct attr attr_tmp = attr;
  2915. info.peer = rsclient;
  2916. info.attr = &attr_tmp;
  2917. SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_EXPORT);
  2918. SET_FLAG (rsclient->rmap_type, PEER_RMAP_TYPE_NETWORK);
  2919. ret = route_map_apply (bgp_static->rmap.map, p, RMAP_BGP, &info);
  2920. rsclient->rmap_type = 0;
  2921. if (ret == RMAP_DENYMATCH)
  2922. {
  2923. /* Free uninterned attribute. */
  2924. bgp_attr_flush (&attr_tmp);
  2925. /* Unintern original. */
  2926. aspath_unintern (&attr.aspath);
  2927. bgp_static_withdraw_rsclient (bgp, rsclient, p, afi, safi);
  2928. bgp_attr_extra_free (&attr);
  2929. return;
  2930. }
  2931. attr_new = bgp_attr_intern (&attr_tmp);
  2932. }
  2933. else
  2934. attr_new = bgp_attr_intern (&attr);
  2935. new_attr.extra = &new_extra;
  2936. bgp_attr_dup(&new_attr, attr_new);
  2937. SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
  2938. if (bgp_import_modifier (rsclient, bgp->peer_self, p, &new_attr, afi, safi)
  2939. == RMAP_DENY)
  2940. {
  2941. /* This BGP update is filtered. Log the reason then update BGP entry. */
  2942. if (BGP_DEBUG (update, UPDATE_IN))
  2943. zlog (rsclient->log, LOG_DEBUG,
  2944. "Static UPDATE about %s/%d -- DENIED for RS-client %s due to: import-policy",
  2945. inet_ntop (p->family, &p->u.prefix, buf, SU_ADDRSTRLEN),
  2946. p->prefixlen, rsclient->host);
  2947. bgp->peer_self->rmap_type = 0;
  2948. bgp_attr_unintern (&attr_new);
  2949. aspath_unintern (&attr.aspath);
  2950. bgp_attr_extra_free (&attr);
  2951. bgp_static_withdraw_rsclient (bgp, rsclient, p, afi, safi);
  2952. return;
  2953. }
  2954. bgp->peer_self->rmap_type = 0;
  2955. bgp_attr_unintern (&attr_new);
  2956. attr_new = bgp_attr_intern (&new_attr);
  2957. for (ri = rn->info; ri; ri = ri->next)
  2958. if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
  2959. && ri->sub_type == BGP_ROUTE_STATIC)
  2960. break;
  2961. if (ri)
  2962. {
  2963. if (attrhash_cmp (ri->attr, attr_new) &&
  2964. !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  2965. {
  2966. bgp_unlock_node (rn);
  2967. bgp_attr_unintern (&attr_new);
  2968. aspath_unintern (&attr.aspath);
  2969. bgp_attr_extra_free (&attr);
  2970. return;
  2971. }
  2972. else
  2973. {
  2974. /* The attribute is changed. */
  2975. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  2976. /* Rewrite BGP route information. */
  2977. if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  2978. bgp_info_restore(rn, ri);
  2979. bgp_attr_unintern (&ri->attr);
  2980. ri->attr = attr_new;
  2981. ri->uptime = bgp_clock ();
  2982. /* Nexthop reachability check. */
  2983. if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK))
  2984. {
  2985. if (bgp_find_or_add_nexthop (afi, ri, NULL, 0))
  2986. bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
  2987. else
  2988. {
  2989. if (BGP_DEBUG(nht, NHT))
  2990. {
  2991. char buf1[INET6_ADDRSTRLEN];
  2992. inet_ntop(AF_INET, (const void *)&attr_new->nexthop,
  2993. buf1, INET6_ADDRSTRLEN);
  2994. zlog_debug("%s(%s): NH unresolved", __FUNCTION__, buf1);
  2995. }
  2996. bgp_info_unset_flag (rn, ri, BGP_INFO_VALID);
  2997. }
  2998. }
  2999. /* Process change. */
  3000. bgp_process (bgp, rn, afi, safi);
  3001. bgp_unlock_node (rn);
  3002. aspath_unintern (&attr.aspath);
  3003. bgp_attr_extra_free (&attr);
  3004. return;
  3005. }
  3006. }
  3007. /* Make new BGP info. */
  3008. new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, bgp->peer_self,
  3009. attr_new, rn);
  3010. /* Nexthop reachability check. */
  3011. if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK))
  3012. {
  3013. if (bgp_find_or_add_nexthop (afi, new, NULL, 0))
  3014. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  3015. else
  3016. {
  3017. if (BGP_DEBUG(nht, NHT))
  3018. {
  3019. char buf1[INET6_ADDRSTRLEN];
  3020. inet_ntop(AF_INET, (const void *)&attr_new->nexthop,
  3021. buf1, INET6_ADDRSTRLEN);
  3022. zlog_debug("%s(%s): NH unresolved", __FUNCTION__, buf1);
  3023. }
  3024. bgp_info_unset_flag (rn, new, BGP_INFO_VALID);
  3025. }
  3026. }
  3027. else
  3028. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  3029. /* Register new BGP information. */
  3030. bgp_info_add (rn, new);
  3031. /* route_node_get lock */
  3032. bgp_unlock_node (rn);
  3033. /* Process change. */
  3034. bgp_process (bgp, rn, afi, safi);
  3035. /* Unintern original. */
  3036. aspath_unintern (&attr.aspath);
  3037. bgp_attr_extra_free (&attr);
  3038. }
  3039. static void
  3040. bgp_static_update_main (struct bgp *bgp, struct prefix *p,
  3041. struct bgp_static *bgp_static, afi_t afi, safi_t safi)
  3042. {
  3043. struct bgp_node *rn;
  3044. struct bgp_info *ri;
  3045. struct bgp_info *new;
  3046. struct bgp_info info;
  3047. struct attr attr;
  3048. struct attr *attr_new;
  3049. int ret;
  3050. assert (bgp_static);
  3051. if (!bgp_static)
  3052. return;
  3053. rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, NULL);
  3054. bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
  3055. attr.nexthop = bgp_static->igpnexthop;
  3056. attr.med = bgp_static->igpmetric;
  3057. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
  3058. if (bgp_static->atomic)
  3059. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
  3060. /* Apply route-map. */
  3061. if (bgp_static->rmap.name)
  3062. {
  3063. struct attr attr_tmp = attr;
  3064. info.peer = bgp->peer_self;
  3065. info.attr = &attr_tmp;
  3066. SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
  3067. ret = route_map_apply (bgp_static->rmap.map, p, RMAP_BGP, &info);
  3068. bgp->peer_self->rmap_type = 0;
  3069. if (ret == RMAP_DENYMATCH)
  3070. {
  3071. /* Free uninterned attribute. */
  3072. bgp_attr_flush (&attr_tmp);
  3073. /* Unintern original. */
  3074. aspath_unintern (&attr.aspath);
  3075. bgp_attr_extra_free (&attr);
  3076. bgp_static_withdraw (bgp, p, afi, safi);
  3077. return;
  3078. }
  3079. attr_new = bgp_attr_intern (&attr_tmp);
  3080. }
  3081. else
  3082. attr_new = bgp_attr_intern (&attr);
  3083. for (ri = rn->info; ri; ri = ri->next)
  3084. if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
  3085. && ri->sub_type == BGP_ROUTE_STATIC)
  3086. break;
  3087. if (ri)
  3088. {
  3089. if (attrhash_cmp (ri->attr, attr_new) &&
  3090. !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  3091. {
  3092. bgp_unlock_node (rn);
  3093. bgp_attr_unintern (&attr_new);
  3094. aspath_unintern (&attr.aspath);
  3095. bgp_attr_extra_free (&attr);
  3096. return;
  3097. }
  3098. else
  3099. {
  3100. /* The attribute is changed. */
  3101. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  3102. /* Rewrite BGP route information. */
  3103. if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  3104. bgp_info_restore(rn, ri);
  3105. else
  3106. bgp_aggregate_decrement (bgp, p, ri, afi, safi);
  3107. bgp_attr_unintern (&ri->attr);
  3108. ri->attr = attr_new;
  3109. ri->uptime = bgp_clock ();
  3110. /* Nexthop reachability check. */
  3111. if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK))
  3112. {
  3113. if (bgp_find_or_add_nexthop (afi, ri, NULL, 0))
  3114. bgp_info_set_flag (rn, ri, BGP_INFO_VALID);
  3115. else
  3116. {
  3117. if (BGP_DEBUG(nht, NHT))
  3118. {
  3119. char buf1[INET6_ADDRSTRLEN];
  3120. inet_ntop(AF_INET, (const void *)&attr_new->nexthop,
  3121. buf1, INET6_ADDRSTRLEN);
  3122. zlog_debug("%s(%s): NH unresolved", __FUNCTION__, buf1);
  3123. }
  3124. bgp_info_unset_flag (rn, ri, BGP_INFO_VALID);
  3125. }
  3126. }
  3127. /* Process change. */
  3128. bgp_aggregate_increment (bgp, p, ri, afi, safi);
  3129. bgp_process (bgp, rn, afi, safi);
  3130. bgp_unlock_node (rn);
  3131. aspath_unintern (&attr.aspath);
  3132. bgp_attr_extra_free (&attr);
  3133. return;
  3134. }
  3135. }
  3136. /* Make new BGP info. */
  3137. new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, bgp->peer_self, attr_new,
  3138. rn);
  3139. /* Nexthop reachability check. */
  3140. if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK))
  3141. {
  3142. if (bgp_find_or_add_nexthop (afi, new, NULL, 0))
  3143. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  3144. else
  3145. {
  3146. if (BGP_DEBUG(nht, NHT))
  3147. {
  3148. char buf1[INET6_ADDRSTRLEN];
  3149. inet_ntop(AF_INET, (const void *)&attr_new->nexthop, buf1,
  3150. INET6_ADDRSTRLEN);
  3151. zlog_debug("%s(%s): NH unresolved", __FUNCTION__, buf1);
  3152. }
  3153. bgp_info_unset_flag (rn, new, BGP_INFO_VALID);
  3154. }
  3155. }
  3156. else
  3157. bgp_info_set_flag (rn, new, BGP_INFO_VALID);
  3158. /* Aggregate address increment. */
  3159. bgp_aggregate_increment (bgp, p, new, afi, safi);
  3160. /* Register new BGP information. */
  3161. bgp_info_add (rn, new);
  3162. /* route_node_get lock */
  3163. bgp_unlock_node (rn);
  3164. /* Process change. */
  3165. bgp_process (bgp, rn, afi, safi);
  3166. /* Unintern original. */
  3167. aspath_unintern (&attr.aspath);
  3168. bgp_attr_extra_free (&attr);
  3169. }
  3170. void
  3171. bgp_static_update (struct bgp *bgp, struct prefix *p,
  3172. struct bgp_static *bgp_static, afi_t afi, safi_t safi)
  3173. {
  3174. struct peer *rsclient;
  3175. struct listnode *node, *nnode;
  3176. bgp_static_update_main (bgp, p, bgp_static, afi, safi);
  3177. for (ALL_LIST_ELEMENTS (bgp->rsclient, node, nnode, rsclient))
  3178. {
  3179. if (CHECK_FLAG (rsclient->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  3180. bgp_static_update_rsclient (rsclient, p, bgp_static, afi, safi);
  3181. }
  3182. }
  3183. void
  3184. bgp_static_withdraw (struct bgp *bgp, struct prefix *p, afi_t afi,
  3185. safi_t safi)
  3186. {
  3187. struct bgp_node *rn;
  3188. struct bgp_info *ri;
  3189. /* Make new BGP info. */
  3190. rn = bgp_node_get (bgp->rib[afi][safi], p);
  3191. /* Check selected route and self inserted route. */
  3192. for (ri = rn->info; ri; ri = ri->next)
  3193. if (ri->peer == bgp->peer_self
  3194. && ri->type == ZEBRA_ROUTE_BGP
  3195. && ri->sub_type == BGP_ROUTE_STATIC)
  3196. break;
  3197. /* Withdraw static BGP route from routing table. */
  3198. if (ri)
  3199. {
  3200. bgp_aggregate_decrement (bgp, p, ri, afi, safi);
  3201. bgp_unlink_nexthop(ri);
  3202. bgp_info_delete (rn, ri);
  3203. bgp_process (bgp, rn, afi, safi);
  3204. }
  3205. /* Unlock bgp_node_lookup. */
  3206. bgp_unlock_node (rn);
  3207. }
  3208. void
  3209. bgp_check_local_routes_rsclient (struct peer *rsclient, afi_t afi, safi_t safi)
  3210. {
  3211. struct bgp_static *bgp_static;
  3212. struct bgp *bgp;
  3213. struct bgp_node *rn;
  3214. struct prefix *p;
  3215. bgp = rsclient->bgp;
  3216. for (rn = bgp_table_top (bgp->route[afi][safi]); rn; rn = bgp_route_next (rn))
  3217. if ((bgp_static = rn->info) != NULL)
  3218. {
  3219. p = &rn->p;
  3220. bgp_static_update_rsclient (rsclient, p, bgp_static,
  3221. afi, safi);
  3222. }
  3223. }
  3224. /*
  3225. * Used for SAFI_MPLS_VPN and SAFI_ENCAP
  3226. */
  3227. static void
  3228. bgp_static_withdraw_safi (struct bgp *bgp, struct prefix *p, afi_t afi,
  3229. safi_t safi, struct prefix_rd *prd, u_char *tag)
  3230. {
  3231. struct bgp_node *rn;
  3232. struct bgp_info *ri;
  3233. rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, prd);
  3234. /* Check selected route and self inserted route. */
  3235. for (ri = rn->info; ri; ri = ri->next)
  3236. if (ri->peer == bgp->peer_self
  3237. && ri->type == ZEBRA_ROUTE_BGP
  3238. && ri->sub_type == BGP_ROUTE_STATIC)
  3239. break;
  3240. /* Withdraw static BGP route from routing table. */
  3241. if (ri)
  3242. {
  3243. bgp_aggregate_decrement (bgp, p, ri, afi, safi);
  3244. bgp_info_delete (rn, ri);
  3245. bgp_process (bgp, rn, afi, safi);
  3246. }
  3247. /* Unlock bgp_node_lookup. */
  3248. bgp_unlock_node (rn);
  3249. }
  3250. static void
  3251. bgp_static_update_safi (struct bgp *bgp, struct prefix *p,
  3252. struct bgp_static *bgp_static, afi_t afi, safi_t safi)
  3253. {
  3254. struct bgp_node *rn;
  3255. struct bgp_info *new;
  3256. struct attr *attr_new;
  3257. struct attr attr = { 0 };
  3258. struct bgp_info *ri;
  3259. assert (bgp_static);
  3260. rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, p, &bgp_static->prd);
  3261. bgp_attr_default_set (&attr, BGP_ORIGIN_IGP);
  3262. attr.nexthop = bgp_static->igpnexthop;
  3263. attr.med = bgp_static->igpmetric;
  3264. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
  3265. /* Apply route-map. */
  3266. if (bgp_static->rmap.name)
  3267. {
  3268. struct attr attr_tmp = attr;
  3269. struct bgp_info info;
  3270. int ret;
  3271. info.peer = bgp->peer_self;
  3272. info.attr = &attr_tmp;
  3273. SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
  3274. ret = route_map_apply (bgp_static->rmap.map, p, RMAP_BGP, &info);
  3275. bgp->peer_self->rmap_type = 0;
  3276. if (ret == RMAP_DENYMATCH)
  3277. {
  3278. /* Free uninterned attribute. */
  3279. bgp_attr_flush (&attr_tmp);
  3280. /* Unintern original. */
  3281. aspath_unintern (&attr.aspath);
  3282. bgp_attr_extra_free (&attr);
  3283. bgp_static_withdraw_safi (bgp, p, afi, safi, &bgp_static->prd,
  3284. bgp_static->tag);
  3285. return;
  3286. }
  3287. attr_new = bgp_attr_intern (&attr_tmp);
  3288. }
  3289. else
  3290. {
  3291. attr_new = bgp_attr_intern (&attr);
  3292. }
  3293. for (ri = rn->info; ri; ri = ri->next)
  3294. if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
  3295. && ri->sub_type == BGP_ROUTE_STATIC)
  3296. break;
  3297. if (ri)
  3298. {
  3299. if (attrhash_cmp (ri->attr, attr_new) &&
  3300. !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  3301. {
  3302. bgp_unlock_node (rn);
  3303. bgp_attr_unintern (&attr_new);
  3304. aspath_unintern (&attr.aspath);
  3305. bgp_attr_extra_free (&attr);
  3306. return;
  3307. }
  3308. else
  3309. {
  3310. /* The attribute is changed. */
  3311. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  3312. /* Rewrite BGP route information. */
  3313. if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
  3314. bgp_info_restore(rn, ri);
  3315. else
  3316. bgp_aggregate_decrement (bgp, p, ri, afi, safi);
  3317. bgp_attr_unintern (&ri->attr);
  3318. ri->attr = attr_new;
  3319. ri->uptime = bgp_clock ();
  3320. /* Process change. */
  3321. bgp_aggregate_increment (bgp, p, ri, afi, safi);
  3322. bgp_process (bgp, rn, afi, safi);
  3323. bgp_unlock_node (rn);
  3324. aspath_unintern (&attr.aspath);
  3325. bgp_attr_extra_free (&attr);
  3326. return;
  3327. }
  3328. }
  3329. /* Make new BGP info. */
  3330. new = info_make (ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, bgp->peer_self,
  3331. attr_new, rn);
  3332. SET_FLAG (new->flags, BGP_INFO_VALID);
  3333. new->extra = bgp_info_extra_new();
  3334. memcpy (new->extra->tag, bgp_static->tag, 3);
  3335. /* Aggregate address increment. */
  3336. bgp_aggregate_increment (bgp, p, new, afi, safi);
  3337. /* Register new BGP information. */
  3338. bgp_info_add (rn, new);
  3339. /* route_node_get lock */
  3340. bgp_unlock_node (rn);
  3341. /* Process change. */
  3342. bgp_process (bgp, rn, afi, safi);
  3343. /* Unintern original. */
  3344. aspath_unintern (&attr.aspath);
  3345. bgp_attr_extra_free (&attr);
  3346. }
  3347. /* Configure static BGP network. When user don't run zebra, static
  3348. route should be installed as valid. */
  3349. static int
  3350. bgp_static_set (struct vty *vty, struct bgp *bgp, const char *ip_str,
  3351. afi_t afi, safi_t safi, const char *rmap, int backdoor)
  3352. {
  3353. int ret;
  3354. struct prefix p;
  3355. struct bgp_static *bgp_static;
  3356. struct bgp_node *rn;
  3357. u_char need_update = 0;
  3358. /* Convert IP prefix string to struct prefix. */
  3359. ret = str2prefix (ip_str, &p);
  3360. if (! ret)
  3361. {
  3362. vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
  3363. return CMD_WARNING;
  3364. }
  3365. if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
  3366. {
  3367. vty_out (vty, "%% Malformed prefix (link-local address)%s",
  3368. VTY_NEWLINE);
  3369. return CMD_WARNING;
  3370. }
  3371. apply_mask (&p);
  3372. /* Set BGP static route configuration. */
  3373. rn = bgp_node_get (bgp->route[afi][safi], &p);
  3374. if (rn->info)
  3375. {
  3376. /* Configuration change. */
  3377. bgp_static = rn->info;
  3378. /* Check previous routes are installed into BGP. */
  3379. if (bgp_static->valid && bgp_static->backdoor != backdoor)
  3380. need_update = 1;
  3381. bgp_static->backdoor = backdoor;
  3382. if (rmap)
  3383. {
  3384. if (bgp_static->rmap.name)
  3385. free (bgp_static->rmap.name);
  3386. bgp_static->rmap.name = strdup (rmap);
  3387. bgp_static->rmap.map = route_map_lookup_by_name (rmap);
  3388. }
  3389. else
  3390. {
  3391. if (bgp_static->rmap.name)
  3392. free (bgp_static->rmap.name);
  3393. bgp_static->rmap.name = NULL;
  3394. bgp_static->rmap.map = NULL;
  3395. bgp_static->valid = 0;
  3396. }
  3397. bgp_unlock_node (rn);
  3398. }
  3399. else
  3400. {
  3401. /* New configuration. */
  3402. bgp_static = bgp_static_new ();
  3403. bgp_static->backdoor = backdoor;
  3404. bgp_static->valid = 0;
  3405. bgp_static->igpmetric = 0;
  3406. bgp_static->igpnexthop.s_addr = 0;
  3407. if (rmap)
  3408. {
  3409. if (bgp_static->rmap.name)
  3410. free (bgp_static->rmap.name);
  3411. bgp_static->rmap.name = strdup (rmap);
  3412. bgp_static->rmap.map = route_map_lookup_by_name (rmap);
  3413. }
  3414. rn->info = bgp_static;
  3415. }
  3416. bgp_static->valid = 1;
  3417. if (need_update)
  3418. bgp_static_withdraw (bgp, &p, afi, safi);
  3419. if (! bgp_static->backdoor)
  3420. bgp_static_update (bgp, &p, bgp_static, afi, safi);
  3421. return CMD_SUCCESS;
  3422. }
  3423. /* Configure static BGP network. */
  3424. static int
  3425. bgp_static_unset (struct vty *vty, struct bgp *bgp, const char *ip_str,
  3426. afi_t afi, safi_t safi)
  3427. {
  3428. int ret;
  3429. struct prefix p;
  3430. struct bgp_static *bgp_static;
  3431. struct bgp_node *rn;
  3432. /* Convert IP prefix string to struct prefix. */
  3433. ret = str2prefix (ip_str, &p);
  3434. if (! ret)
  3435. {
  3436. vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
  3437. return CMD_WARNING;
  3438. }
  3439. if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
  3440. {
  3441. vty_out (vty, "%% Malformed prefix (link-local address)%s",
  3442. VTY_NEWLINE);
  3443. return CMD_WARNING;
  3444. }
  3445. apply_mask (&p);
  3446. rn = bgp_node_lookup (bgp->route[afi][safi], &p);
  3447. if (! rn)
  3448. {
  3449. vty_out (vty, "%% Can't find specified static route configuration.%s",
  3450. VTY_NEWLINE);
  3451. return CMD_WARNING;
  3452. }
  3453. bgp_static = rn->info;
  3454. /* Update BGP RIB. */
  3455. if (! bgp_static->backdoor)
  3456. bgp_static_withdraw (bgp, &p, afi, safi);
  3457. /* Clear configuration. */
  3458. bgp_static_free (bgp_static);
  3459. rn->info = NULL;
  3460. bgp_unlock_node (rn);
  3461. bgp_unlock_node (rn);
  3462. return CMD_SUCCESS;
  3463. }
  3464. /* Called from bgp_delete(). Delete all static routes from the BGP
  3465. instance. */
  3466. void
  3467. bgp_static_delete (struct bgp *bgp)
  3468. {
  3469. afi_t afi;
  3470. safi_t safi;
  3471. struct bgp_node *rn;
  3472. struct bgp_node *rm;
  3473. struct bgp_table *table;
  3474. struct bgp_static *bgp_static;
  3475. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3476. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3477. for (rn = bgp_table_top (bgp->route[afi][safi]); rn; rn = bgp_route_next (rn))
  3478. if (rn->info != NULL)
  3479. {
  3480. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  3481. {
  3482. table = rn->info;
  3483. for (rm = bgp_table_top (table); rm; rm = bgp_route_next (rm))
  3484. {
  3485. bgp_static = rn->info;
  3486. bgp_static_withdraw_safi (bgp, &rm->p,
  3487. AFI_IP, safi,
  3488. (struct prefix_rd *)&rn->p,
  3489. bgp_static->tag);
  3490. bgp_static_free (bgp_static);
  3491. rn->info = NULL;
  3492. bgp_unlock_node (rn);
  3493. }
  3494. }
  3495. else
  3496. {
  3497. bgp_static = rn->info;
  3498. bgp_static_withdraw (bgp, &rn->p, afi, safi);
  3499. bgp_static_free (bgp_static);
  3500. rn->info = NULL;
  3501. bgp_unlock_node (rn);
  3502. }
  3503. }
  3504. }
  3505. /*
  3506. * gpz 110624
  3507. * Currently this is used to set static routes for VPN and ENCAP.
  3508. * I think it can probably be factored with bgp_static_set.
  3509. */
  3510. int
  3511. bgp_static_set_safi (safi_t safi, struct vty *vty, const char *ip_str,
  3512. const char *rd_str, const char *tag_str,
  3513. const char *rmap_str)
  3514. {
  3515. int ret;
  3516. struct prefix p;
  3517. struct prefix_rd prd;
  3518. struct bgp *bgp;
  3519. struct bgp_node *prn;
  3520. struct bgp_node *rn;
  3521. struct bgp_table *table;
  3522. struct bgp_static *bgp_static;
  3523. u_char tag[3];
  3524. bgp = vty->index;
  3525. ret = str2prefix (ip_str, &p);
  3526. if (! ret)
  3527. {
  3528. vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
  3529. return CMD_WARNING;
  3530. }
  3531. apply_mask (&p);
  3532. ret = str2prefix_rd (rd_str, &prd);
  3533. if (! ret)
  3534. {
  3535. vty_out (vty, "%% Malformed rd%s", VTY_NEWLINE);
  3536. return CMD_WARNING;
  3537. }
  3538. ret = str2tag (tag_str, tag);
  3539. if (! ret)
  3540. {
  3541. vty_out (vty, "%% Malformed tag%s", VTY_NEWLINE);
  3542. return CMD_WARNING;
  3543. }
  3544. prn = bgp_node_get (bgp->route[AFI_IP][safi],
  3545. (struct prefix *)&prd);
  3546. if (prn->info == NULL)
  3547. prn->info = bgp_table_init (AFI_IP, safi);
  3548. else
  3549. bgp_unlock_node (prn);
  3550. table = prn->info;
  3551. rn = bgp_node_get (table, &p);
  3552. if (rn->info)
  3553. {
  3554. vty_out (vty, "%% Same network configuration exists%s", VTY_NEWLINE);
  3555. bgp_unlock_node (rn);
  3556. }
  3557. else
  3558. {
  3559. /* New configuration. */
  3560. bgp_static = bgp_static_new ();
  3561. bgp_static->backdoor = 0;
  3562. bgp_static->valid = 0;
  3563. bgp_static->igpmetric = 0;
  3564. bgp_static->igpnexthop.s_addr = 0;
  3565. memcpy(bgp_static->tag, tag, 3);
  3566. bgp_static->prd = prd;
  3567. if (rmap_str)
  3568. {
  3569. if (bgp_static->rmap.name)
  3570. free (bgp_static->rmap.name);
  3571. bgp_static->rmap.name = strdup (rmap_str);
  3572. bgp_static->rmap.map = route_map_lookup_by_name (rmap_str);
  3573. }
  3574. rn->info = bgp_static;
  3575. bgp_static->valid = 1;
  3576. bgp_static_update_safi (bgp, &p, bgp_static, AFI_IP, safi);
  3577. }
  3578. return CMD_SUCCESS;
  3579. }
  3580. /* Configure static BGP network. */
  3581. int
  3582. bgp_static_unset_safi(safi_t safi, struct vty *vty, const char *ip_str,
  3583. const char *rd_str, const char *tag_str)
  3584. {
  3585. int ret;
  3586. struct bgp *bgp;
  3587. struct prefix p;
  3588. struct prefix_rd prd;
  3589. struct bgp_node *prn;
  3590. struct bgp_node *rn;
  3591. struct bgp_table *table;
  3592. struct bgp_static *bgp_static;
  3593. u_char tag[3];
  3594. bgp = vty->index;
  3595. /* Convert IP prefix string to struct prefix. */
  3596. ret = str2prefix (ip_str, &p);
  3597. if (! ret)
  3598. {
  3599. vty_out (vty, "%% Malformed prefix%s", VTY_NEWLINE);
  3600. return CMD_WARNING;
  3601. }
  3602. apply_mask (&p);
  3603. ret = str2prefix_rd (rd_str, &prd);
  3604. if (! ret)
  3605. {
  3606. vty_out (vty, "%% Malformed rd%s", VTY_NEWLINE);
  3607. return CMD_WARNING;
  3608. }
  3609. ret = str2tag (tag_str, tag);
  3610. if (! ret)
  3611. {
  3612. vty_out (vty, "%% Malformed tag%s", VTY_NEWLINE);
  3613. return CMD_WARNING;
  3614. }
  3615. prn = bgp_node_get (bgp->route[AFI_IP][safi],
  3616. (struct prefix *)&prd);
  3617. if (prn->info == NULL)
  3618. prn->info = bgp_table_init (AFI_IP, safi);
  3619. else
  3620. bgp_unlock_node (prn);
  3621. table = prn->info;
  3622. rn = bgp_node_lookup (table, &p);
  3623. if (rn)
  3624. {
  3625. bgp_static_withdraw_safi (bgp, &p, AFI_IP, safi, &prd, tag);
  3626. bgp_static = rn->info;
  3627. bgp_static_free (bgp_static);
  3628. rn->info = NULL;
  3629. bgp_unlock_node (rn);
  3630. bgp_unlock_node (rn);
  3631. }
  3632. else
  3633. vty_out (vty, "%% Can't find the route%s", VTY_NEWLINE);
  3634. return CMD_SUCCESS;
  3635. }
  3636. DEFUN (bgp_network,
  3637. bgp_network_cmd,
  3638. "network A.B.C.D/M",
  3639. "Specify a network to announce via BGP\n"
  3640. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  3641. {
  3642. return bgp_static_set (vty, vty->index, argv[0],
  3643. AFI_IP, bgp_node_safi (vty), NULL, 0);
  3644. }
  3645. DEFUN (bgp_network_route_map,
  3646. bgp_network_route_map_cmd,
  3647. "network A.B.C.D/M route-map WORD",
  3648. "Specify a network to announce via BGP\n"
  3649. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3650. "Route-map to modify the attributes\n"
  3651. "Name of the route map\n")
  3652. {
  3653. return bgp_static_set (vty, vty->index, argv[0],
  3654. AFI_IP, bgp_node_safi (vty), argv[1], 0);
  3655. }
  3656. DEFUN (bgp_network_backdoor,
  3657. bgp_network_backdoor_cmd,
  3658. "network A.B.C.D/M backdoor",
  3659. "Specify a network to announce via BGP\n"
  3660. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3661. "Specify a BGP backdoor route\n")
  3662. {
  3663. return bgp_static_set (vty, vty->index, argv[0], AFI_IP, SAFI_UNICAST,
  3664. NULL, 1);
  3665. }
  3666. DEFUN (bgp_network_mask,
  3667. bgp_network_mask_cmd,
  3668. "network A.B.C.D mask A.B.C.D",
  3669. "Specify a network to announce via BGP\n"
  3670. "Network number\n"
  3671. "Network mask\n"
  3672. "Network mask\n")
  3673. {
  3674. int ret;
  3675. char prefix_str[BUFSIZ];
  3676. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  3677. if (! ret)
  3678. {
  3679. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3680. return CMD_WARNING;
  3681. }
  3682. return bgp_static_set (vty, vty->index, prefix_str,
  3683. AFI_IP, bgp_node_safi (vty), NULL, 0);
  3684. }
  3685. DEFUN (bgp_network_mask_route_map,
  3686. bgp_network_mask_route_map_cmd,
  3687. "network A.B.C.D mask A.B.C.D route-map WORD",
  3688. "Specify a network to announce via BGP\n"
  3689. "Network number\n"
  3690. "Network mask\n"
  3691. "Network mask\n"
  3692. "Route-map to modify the attributes\n"
  3693. "Name of the route map\n")
  3694. {
  3695. int ret;
  3696. char prefix_str[BUFSIZ];
  3697. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  3698. if (! ret)
  3699. {
  3700. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3701. return CMD_WARNING;
  3702. }
  3703. return bgp_static_set (vty, vty->index, prefix_str,
  3704. AFI_IP, bgp_node_safi (vty), argv[2], 0);
  3705. }
  3706. DEFUN (bgp_network_mask_backdoor,
  3707. bgp_network_mask_backdoor_cmd,
  3708. "network A.B.C.D mask A.B.C.D backdoor",
  3709. "Specify a network to announce via BGP\n"
  3710. "Network number\n"
  3711. "Network mask\n"
  3712. "Network mask\n"
  3713. "Specify a BGP backdoor route\n")
  3714. {
  3715. int ret;
  3716. char prefix_str[BUFSIZ];
  3717. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  3718. if (! ret)
  3719. {
  3720. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3721. return CMD_WARNING;
  3722. }
  3723. return bgp_static_set (vty, vty->index, prefix_str, AFI_IP, SAFI_UNICAST,
  3724. NULL, 1);
  3725. }
  3726. DEFUN (bgp_network_mask_natural,
  3727. bgp_network_mask_natural_cmd,
  3728. "network A.B.C.D",
  3729. "Specify a network to announce via BGP\n"
  3730. "Network number\n")
  3731. {
  3732. int ret;
  3733. char prefix_str[BUFSIZ];
  3734. ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
  3735. if (! ret)
  3736. {
  3737. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3738. return CMD_WARNING;
  3739. }
  3740. return bgp_static_set (vty, vty->index, prefix_str,
  3741. AFI_IP, bgp_node_safi (vty), NULL, 0);
  3742. }
  3743. DEFUN (bgp_network_mask_natural_route_map,
  3744. bgp_network_mask_natural_route_map_cmd,
  3745. "network A.B.C.D route-map WORD",
  3746. "Specify a network to announce via BGP\n"
  3747. "Network number\n"
  3748. "Route-map to modify the attributes\n"
  3749. "Name of the route map\n")
  3750. {
  3751. int ret;
  3752. char prefix_str[BUFSIZ];
  3753. ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
  3754. if (! ret)
  3755. {
  3756. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3757. return CMD_WARNING;
  3758. }
  3759. return bgp_static_set (vty, vty->index, prefix_str,
  3760. AFI_IP, bgp_node_safi (vty), argv[1], 0);
  3761. }
  3762. DEFUN (bgp_network_mask_natural_backdoor,
  3763. bgp_network_mask_natural_backdoor_cmd,
  3764. "network A.B.C.D backdoor",
  3765. "Specify a network to announce via BGP\n"
  3766. "Network number\n"
  3767. "Specify a BGP backdoor route\n")
  3768. {
  3769. int ret;
  3770. char prefix_str[BUFSIZ];
  3771. ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
  3772. if (! ret)
  3773. {
  3774. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3775. return CMD_WARNING;
  3776. }
  3777. return bgp_static_set (vty, vty->index, prefix_str, AFI_IP, SAFI_UNICAST,
  3778. NULL, 1);
  3779. }
  3780. DEFUN (no_bgp_network,
  3781. no_bgp_network_cmd,
  3782. "no network A.B.C.D/M",
  3783. NO_STR
  3784. "Specify a network to announce via BGP\n"
  3785. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  3786. {
  3787. return bgp_static_unset (vty, vty->index, argv[0], AFI_IP,
  3788. bgp_node_safi (vty));
  3789. }
  3790. ALIAS (no_bgp_network,
  3791. no_bgp_network_route_map_cmd,
  3792. "no network A.B.C.D/M route-map WORD",
  3793. NO_STR
  3794. "Specify a network to announce via BGP\n"
  3795. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3796. "Route-map to modify the attributes\n"
  3797. "Name of the route map\n")
  3798. ALIAS (no_bgp_network,
  3799. no_bgp_network_backdoor_cmd,
  3800. "no network A.B.C.D/M backdoor",
  3801. NO_STR
  3802. "Specify a network to announce via BGP\n"
  3803. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3804. "Specify a BGP backdoor route\n")
  3805. DEFUN (no_bgp_network_mask,
  3806. no_bgp_network_mask_cmd,
  3807. "no network A.B.C.D mask A.B.C.D",
  3808. NO_STR
  3809. "Specify a network to announce via BGP\n"
  3810. "Network number\n"
  3811. "Network mask\n"
  3812. "Network mask\n")
  3813. {
  3814. int ret;
  3815. char prefix_str[BUFSIZ];
  3816. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  3817. if (! ret)
  3818. {
  3819. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3820. return CMD_WARNING;
  3821. }
  3822. return bgp_static_unset (vty, vty->index, prefix_str, AFI_IP,
  3823. bgp_node_safi (vty));
  3824. }
  3825. ALIAS (no_bgp_network_mask,
  3826. no_bgp_network_mask_route_map_cmd,
  3827. "no network A.B.C.D mask A.B.C.D route-map WORD",
  3828. NO_STR
  3829. "Specify a network to announce via BGP\n"
  3830. "Network number\n"
  3831. "Network mask\n"
  3832. "Network mask\n"
  3833. "Route-map to modify the attributes\n"
  3834. "Name of the route map\n")
  3835. ALIAS (no_bgp_network_mask,
  3836. no_bgp_network_mask_backdoor_cmd,
  3837. "no network A.B.C.D mask A.B.C.D backdoor",
  3838. NO_STR
  3839. "Specify a network to announce via BGP\n"
  3840. "Network number\n"
  3841. "Network mask\n"
  3842. "Network mask\n"
  3843. "Specify a BGP backdoor route\n")
  3844. DEFUN (no_bgp_network_mask_natural,
  3845. no_bgp_network_mask_natural_cmd,
  3846. "no network A.B.C.D",
  3847. NO_STR
  3848. "Specify a network to announce via BGP\n"
  3849. "Network number\n")
  3850. {
  3851. int ret;
  3852. char prefix_str[BUFSIZ];
  3853. ret = netmask_str2prefix_str (argv[0], NULL, prefix_str);
  3854. if (! ret)
  3855. {
  3856. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  3857. return CMD_WARNING;
  3858. }
  3859. return bgp_static_unset (vty, vty->index, prefix_str, AFI_IP,
  3860. bgp_node_safi (vty));
  3861. }
  3862. ALIAS (no_bgp_network_mask_natural,
  3863. no_bgp_network_mask_natural_route_map_cmd,
  3864. "no network A.B.C.D route-map WORD",
  3865. NO_STR
  3866. "Specify a network to announce via BGP\n"
  3867. "Network number\n"
  3868. "Route-map to modify the attributes\n"
  3869. "Name of the route map\n")
  3870. ALIAS (no_bgp_network_mask_natural,
  3871. no_bgp_network_mask_natural_backdoor_cmd,
  3872. "no network A.B.C.D backdoor",
  3873. NO_STR
  3874. "Specify a network to announce via BGP\n"
  3875. "Network number\n"
  3876. "Specify a BGP backdoor route\n")
  3877. DEFUN (ipv6_bgp_network,
  3878. ipv6_bgp_network_cmd,
  3879. "network X:X::X:X/M",
  3880. "Specify a network to announce via BGP\n"
  3881. "IPv6 prefix <network>/<length>\n")
  3882. {
  3883. return bgp_static_set (vty, vty->index, argv[0], AFI_IP6, bgp_node_safi(vty),
  3884. NULL, 0);
  3885. }
  3886. DEFUN (ipv6_bgp_network_route_map,
  3887. ipv6_bgp_network_route_map_cmd,
  3888. "network X:X::X:X/M route-map WORD",
  3889. "Specify a network to announce via BGP\n"
  3890. "IPv6 prefix <network>/<length>\n"
  3891. "Route-map to modify the attributes\n"
  3892. "Name of the route map\n")
  3893. {
  3894. return bgp_static_set (vty, vty->index, argv[0], AFI_IP6,
  3895. bgp_node_safi (vty), argv[1], 0);
  3896. }
  3897. DEFUN (no_ipv6_bgp_network,
  3898. no_ipv6_bgp_network_cmd,
  3899. "no network X:X::X:X/M",
  3900. NO_STR
  3901. "Specify a network to announce via BGP\n"
  3902. "IPv6 prefix <network>/<length>\n")
  3903. {
  3904. return bgp_static_unset (vty, vty->index, argv[0], AFI_IP6, bgp_node_safi(vty));
  3905. }
  3906. ALIAS (no_ipv6_bgp_network,
  3907. no_ipv6_bgp_network_route_map_cmd,
  3908. "no network X:X::X:X/M route-map WORD",
  3909. NO_STR
  3910. "Specify a network to announce via BGP\n"
  3911. "IPv6 prefix <network>/<length>\n"
  3912. "Route-map to modify the attributes\n"
  3913. "Name of the route map\n")
  3914. ALIAS (ipv6_bgp_network,
  3915. old_ipv6_bgp_network_cmd,
  3916. "ipv6 bgp network X:X::X:X/M",
  3917. IPV6_STR
  3918. BGP_STR
  3919. "Specify a network to announce via BGP\n"
  3920. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
  3921. ALIAS (no_ipv6_bgp_network,
  3922. old_no_ipv6_bgp_network_cmd,
  3923. "no ipv6 bgp network X:X::X:X/M",
  3924. NO_STR
  3925. IPV6_STR
  3926. BGP_STR
  3927. "Specify a network to announce via BGP\n"
  3928. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
  3929. /* stubs for removed AS-Pathlimit commands, kept for config compatibility */
  3930. ALIAS_DEPRECATED (bgp_network,
  3931. bgp_network_ttl_cmd,
  3932. "network A.B.C.D/M pathlimit <0-255>",
  3933. "Specify a network to announce via BGP\n"
  3934. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3935. "AS-Path hopcount limit attribute\n"
  3936. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3937. ALIAS_DEPRECATED (bgp_network_backdoor,
  3938. bgp_network_backdoor_ttl_cmd,
  3939. "network A.B.C.D/M backdoor pathlimit <0-255>",
  3940. "Specify a network to announce via BGP\n"
  3941. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3942. "Specify a BGP backdoor route\n"
  3943. "AS-Path hopcount limit attribute\n"
  3944. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3945. ALIAS_DEPRECATED (bgp_network_mask,
  3946. bgp_network_mask_ttl_cmd,
  3947. "network A.B.C.D mask A.B.C.D pathlimit <0-255>",
  3948. "Specify a network to announce via BGP\n"
  3949. "Network number\n"
  3950. "Network mask\n"
  3951. "Network mask\n"
  3952. "AS-Path hopcount limit attribute\n"
  3953. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3954. ALIAS_DEPRECATED (bgp_network_mask_backdoor,
  3955. bgp_network_mask_backdoor_ttl_cmd,
  3956. "network A.B.C.D mask A.B.C.D backdoor pathlimit <0-255>",
  3957. "Specify a network to announce via BGP\n"
  3958. "Network number\n"
  3959. "Network mask\n"
  3960. "Network mask\n"
  3961. "Specify a BGP backdoor route\n"
  3962. "AS-Path hopcount limit attribute\n"
  3963. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3964. ALIAS_DEPRECATED (bgp_network_mask_natural,
  3965. bgp_network_mask_natural_ttl_cmd,
  3966. "network A.B.C.D pathlimit <0-255>",
  3967. "Specify a network to announce via BGP\n"
  3968. "Network number\n"
  3969. "AS-Path hopcount limit attribute\n"
  3970. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3971. ALIAS_DEPRECATED (bgp_network_mask_natural_backdoor,
  3972. bgp_network_mask_natural_backdoor_ttl_cmd,
  3973. "network A.B.C.D backdoor pathlimit <1-255>",
  3974. "Specify a network to announce via BGP\n"
  3975. "Network number\n"
  3976. "Specify a BGP backdoor route\n"
  3977. "AS-Path hopcount limit attribute\n"
  3978. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3979. ALIAS_DEPRECATED (no_bgp_network,
  3980. no_bgp_network_ttl_cmd,
  3981. "no network A.B.C.D/M pathlimit <0-255>",
  3982. NO_STR
  3983. "Specify a network to announce via BGP\n"
  3984. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3985. "AS-Path hopcount limit attribute\n"
  3986. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3987. ALIAS_DEPRECATED (no_bgp_network,
  3988. no_bgp_network_backdoor_ttl_cmd,
  3989. "no network A.B.C.D/M backdoor pathlimit <0-255>",
  3990. NO_STR
  3991. "Specify a network to announce via BGP\n"
  3992. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  3993. "Specify a BGP backdoor route\n"
  3994. "AS-Path hopcount limit attribute\n"
  3995. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  3996. ALIAS_DEPRECATED (no_bgp_network,
  3997. no_bgp_network_mask_ttl_cmd,
  3998. "no network A.B.C.D mask A.B.C.D pathlimit <0-255>",
  3999. NO_STR
  4000. "Specify a network to announce via BGP\n"
  4001. "Network number\n"
  4002. "Network mask\n"
  4003. "Network mask\n"
  4004. "AS-Path hopcount limit attribute\n"
  4005. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  4006. ALIAS_DEPRECATED (no_bgp_network_mask,
  4007. no_bgp_network_mask_backdoor_ttl_cmd,
  4008. "no network A.B.C.D mask A.B.C.D backdoor pathlimit <0-255>",
  4009. NO_STR
  4010. "Specify a network to announce via BGP\n"
  4011. "Network number\n"
  4012. "Network mask\n"
  4013. "Network mask\n"
  4014. "Specify a BGP backdoor route\n"
  4015. "AS-Path hopcount limit attribute\n"
  4016. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  4017. ALIAS_DEPRECATED (no_bgp_network_mask_natural,
  4018. no_bgp_network_mask_natural_ttl_cmd,
  4019. "no network A.B.C.D pathlimit <0-255>",
  4020. NO_STR
  4021. "Specify a network to announce via BGP\n"
  4022. "Network number\n"
  4023. "AS-Path hopcount limit attribute\n"
  4024. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  4025. ALIAS_DEPRECATED (no_bgp_network_mask_natural,
  4026. no_bgp_network_mask_natural_backdoor_ttl_cmd,
  4027. "no network A.B.C.D backdoor pathlimit <0-255>",
  4028. NO_STR
  4029. "Specify a network to announce via BGP\n"
  4030. "Network number\n"
  4031. "Specify a BGP backdoor route\n"
  4032. "AS-Path hopcount limit attribute\n"
  4033. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  4034. ALIAS_DEPRECATED (ipv6_bgp_network,
  4035. ipv6_bgp_network_ttl_cmd,
  4036. "network X:X::X:X/M pathlimit <0-255>",
  4037. "Specify a network to announce via BGP\n"
  4038. "IPv6 prefix <network>/<length>\n"
  4039. "AS-Path hopcount limit attribute\n"
  4040. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  4041. ALIAS_DEPRECATED (no_ipv6_bgp_network,
  4042. no_ipv6_bgp_network_ttl_cmd,
  4043. "no network X:X::X:X/M pathlimit <0-255>",
  4044. NO_STR
  4045. "Specify a network to announce via BGP\n"
  4046. "IPv6 prefix <network>/<length>\n"
  4047. "AS-Path hopcount limit attribute\n"
  4048. "AS-Pathlimit TTL, in number of AS-Path hops\n")
  4049. /* Aggreagete address:
  4050. advertise-map Set condition to advertise attribute
  4051. as-set Generate AS set path information
  4052. attribute-map Set attributes of aggregate
  4053. route-map Set parameters of aggregate
  4054. summary-only Filter more specific routes from updates
  4055. suppress-map Conditionally filter more specific routes from updates
  4056. <cr>
  4057. */
  4058. struct bgp_aggregate
  4059. {
  4060. /* Summary-only flag. */
  4061. u_char summary_only;
  4062. /* AS set generation. */
  4063. u_char as_set;
  4064. /* Route-map for aggregated route. */
  4065. struct route_map *map;
  4066. /* Suppress-count. */
  4067. unsigned long count;
  4068. /* SAFI configuration. */
  4069. safi_t safi;
  4070. };
  4071. static struct bgp_aggregate *
  4072. bgp_aggregate_new (void)
  4073. {
  4074. return XCALLOC (MTYPE_BGP_AGGREGATE, sizeof (struct bgp_aggregate));
  4075. }
  4076. static void
  4077. bgp_aggregate_free (struct bgp_aggregate *aggregate)
  4078. {
  4079. XFREE (MTYPE_BGP_AGGREGATE, aggregate);
  4080. }
  4081. /* Update an aggregate as routes are added/removed from the BGP table */
  4082. static void
  4083. bgp_aggregate_route (struct bgp *bgp, struct prefix *p, struct bgp_info *rinew,
  4084. afi_t afi, safi_t safi, struct bgp_info *del,
  4085. struct bgp_aggregate *aggregate)
  4086. {
  4087. struct bgp_table *table;
  4088. struct bgp_node *top;
  4089. struct bgp_node *rn;
  4090. u_char origin;
  4091. struct aspath *aspath = NULL;
  4092. struct aspath *asmerge = NULL;
  4093. struct community *community = NULL;
  4094. struct community *commerge = NULL;
  4095. struct bgp_info *ri;
  4096. struct bgp_info *new;
  4097. int first = 1;
  4098. unsigned long match = 0;
  4099. u_char atomic_aggregate = 0;
  4100. /* ORIGIN attribute: If at least one route among routes that are
  4101. aggregated has ORIGIN with the value INCOMPLETE, then the
  4102. aggregated route must have the ORIGIN attribute with the value
  4103. INCOMPLETE. Otherwise, if at least one route among routes that
  4104. are aggregated has ORIGIN with the value EGP, then the aggregated
  4105. route must have the origin attribute with the value EGP. In all
  4106. other case the value of the ORIGIN attribute of the aggregated
  4107. route is INTERNAL. */
  4108. origin = BGP_ORIGIN_IGP;
  4109. table = bgp->rib[afi][safi];
  4110. top = bgp_node_get (table, p);
  4111. for (rn = bgp_node_get (table, p); rn; rn = bgp_route_next_until (rn, top))
  4112. if (rn->p.prefixlen > p->prefixlen)
  4113. {
  4114. match = 0;
  4115. for (ri = rn->info; ri; ri = ri->next)
  4116. {
  4117. if (BGP_INFO_HOLDDOWN (ri))
  4118. continue;
  4119. if (del && ri == del)
  4120. continue;
  4121. if (! rinew && first)
  4122. first = 0;
  4123. #ifdef AGGREGATE_NEXTHOP_CHECK
  4124. if (! IPV4_ADDR_SAME (&ri->attr->nexthop, &nexthop)
  4125. || ri->attr->med != med)
  4126. {
  4127. if (aspath)
  4128. aspath_free (aspath);
  4129. if (community)
  4130. community_free (community);
  4131. bgp_unlock_node (rn);
  4132. bgp_unlock_node (top);
  4133. return;
  4134. }
  4135. #endif /* AGGREGATE_NEXTHOP_CHECK */
  4136. if (ri->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  4137. atomic_aggregate = 1;
  4138. if (ri->sub_type != BGP_ROUTE_AGGREGATE)
  4139. {
  4140. if (aggregate->summary_only)
  4141. {
  4142. (bgp_info_extra_get (ri))->suppress++;
  4143. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  4144. match++;
  4145. }
  4146. aggregate->count++;
  4147. if (origin < ri->attr->origin)
  4148. origin = ri->attr->origin;
  4149. if (aggregate->as_set)
  4150. {
  4151. if (aspath)
  4152. {
  4153. asmerge = aspath_aggregate (aspath, ri->attr->aspath);
  4154. aspath_free (aspath);
  4155. aspath = asmerge;
  4156. }
  4157. else
  4158. aspath = aspath_dup (ri->attr->aspath);
  4159. if (ri->attr->community)
  4160. {
  4161. if (community)
  4162. {
  4163. commerge = community_merge (community,
  4164. ri->attr->community);
  4165. community = community_uniq_sort (commerge);
  4166. community_free (commerge);
  4167. }
  4168. else
  4169. community = community_dup (ri->attr->community);
  4170. }
  4171. }
  4172. }
  4173. }
  4174. if (match)
  4175. bgp_process (bgp, rn, afi, safi);
  4176. }
  4177. bgp_unlock_node (top);
  4178. if (rinew)
  4179. {
  4180. aggregate->count++;
  4181. if (aggregate->summary_only)
  4182. (bgp_info_extra_get (rinew))->suppress++;
  4183. if (origin < rinew->attr->origin)
  4184. origin = rinew->attr->origin;
  4185. if (aggregate->as_set)
  4186. {
  4187. if (aspath)
  4188. {
  4189. asmerge = aspath_aggregate (aspath, rinew->attr->aspath);
  4190. aspath_free (aspath);
  4191. aspath = asmerge;
  4192. }
  4193. else
  4194. aspath = aspath_dup (rinew->attr->aspath);
  4195. if (rinew->attr->community)
  4196. {
  4197. if (community)
  4198. {
  4199. commerge = community_merge (community,
  4200. rinew->attr->community);
  4201. community = community_uniq_sort (commerge);
  4202. community_free (commerge);
  4203. }
  4204. else
  4205. community = community_dup (rinew->attr->community);
  4206. }
  4207. }
  4208. }
  4209. if (aggregate->count > 0)
  4210. {
  4211. rn = bgp_node_get (table, p);
  4212. new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_AGGREGATE, bgp->peer_self,
  4213. bgp_attr_aggregate_intern(bgp, origin, aspath, community,
  4214. aggregate->as_set,
  4215. atomic_aggregate), rn);
  4216. SET_FLAG (new->flags, BGP_INFO_VALID);
  4217. bgp_info_add (rn, new);
  4218. bgp_unlock_node (rn);
  4219. bgp_process (bgp, rn, afi, safi);
  4220. }
  4221. else
  4222. {
  4223. if (aspath)
  4224. aspath_free (aspath);
  4225. if (community)
  4226. community_free (community);
  4227. }
  4228. }
  4229. void bgp_aggregate_delete (struct bgp *, struct prefix *, afi_t, safi_t,
  4230. struct bgp_aggregate *);
  4231. void
  4232. bgp_aggregate_increment (struct bgp *bgp, struct prefix *p,
  4233. struct bgp_info *ri, afi_t afi, safi_t safi)
  4234. {
  4235. struct bgp_node *child;
  4236. struct bgp_node *rn;
  4237. struct bgp_aggregate *aggregate;
  4238. struct bgp_table *table;
  4239. /* MPLS-VPN aggregation is not yet supported. */
  4240. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  4241. return;
  4242. table = bgp->aggregate[afi][safi];
  4243. /* No aggregates configured. */
  4244. if (bgp_table_top_nolock (table) == NULL)
  4245. return;
  4246. if (p->prefixlen == 0)
  4247. return;
  4248. if (BGP_INFO_HOLDDOWN (ri))
  4249. return;
  4250. child = bgp_node_get (table, p);
  4251. /* Aggregate address configuration check. */
  4252. for (rn = child; rn; rn = bgp_node_parent_nolock (rn))
  4253. if ((aggregate = rn->info) != NULL && rn->p.prefixlen < p->prefixlen)
  4254. {
  4255. bgp_aggregate_delete (bgp, &rn->p, afi, safi, aggregate);
  4256. bgp_aggregate_route (bgp, &rn->p, ri, afi, safi, NULL, aggregate);
  4257. }
  4258. bgp_unlock_node (child);
  4259. }
  4260. void
  4261. bgp_aggregate_decrement (struct bgp *bgp, struct prefix *p,
  4262. struct bgp_info *del, afi_t afi, safi_t safi)
  4263. {
  4264. struct bgp_node *child;
  4265. struct bgp_node *rn;
  4266. struct bgp_aggregate *aggregate;
  4267. struct bgp_table *table;
  4268. /* MPLS-VPN aggregation is not yet supported. */
  4269. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  4270. return;
  4271. table = bgp->aggregate[afi][safi];
  4272. /* No aggregates configured. */
  4273. if (bgp_table_top_nolock (table) == NULL)
  4274. return;
  4275. if (p->prefixlen == 0)
  4276. return;
  4277. child = bgp_node_get (table, p);
  4278. /* Aggregate address configuration check. */
  4279. for (rn = child; rn; rn = bgp_node_parent_nolock (rn))
  4280. if ((aggregate = rn->info) != NULL && rn->p.prefixlen < p->prefixlen)
  4281. {
  4282. bgp_aggregate_delete (bgp, &rn->p, afi, safi, aggregate);
  4283. bgp_aggregate_route (bgp, &rn->p, NULL, afi, safi, del, aggregate);
  4284. }
  4285. bgp_unlock_node (child);
  4286. }
  4287. /* Called via bgp_aggregate_set when the user configures aggregate-address */
  4288. static void
  4289. bgp_aggregate_add (struct bgp *bgp, struct prefix *p, afi_t afi, safi_t safi,
  4290. struct bgp_aggregate *aggregate)
  4291. {
  4292. struct bgp_table *table;
  4293. struct bgp_node *top;
  4294. struct bgp_node *rn;
  4295. struct bgp_info *new;
  4296. struct bgp_info *ri;
  4297. unsigned long match;
  4298. u_char origin = BGP_ORIGIN_IGP;
  4299. struct aspath *aspath = NULL;
  4300. struct aspath *asmerge = NULL;
  4301. struct community *community = NULL;
  4302. struct community *commerge = NULL;
  4303. u_char atomic_aggregate = 0;
  4304. table = bgp->rib[afi][safi];
  4305. /* Sanity check. */
  4306. if (afi == AFI_IP && p->prefixlen == IPV4_MAX_BITLEN)
  4307. return;
  4308. if (afi == AFI_IP6 && p->prefixlen == IPV6_MAX_BITLEN)
  4309. return;
  4310. /* If routes exists below this node, generate aggregate routes. */
  4311. top = bgp_node_get (table, p);
  4312. for (rn = bgp_node_get (table, p); rn; rn = bgp_route_next_until (rn, top))
  4313. if (rn->p.prefixlen > p->prefixlen)
  4314. {
  4315. match = 0;
  4316. for (ri = rn->info; ri; ri = ri->next)
  4317. {
  4318. if (BGP_INFO_HOLDDOWN (ri))
  4319. continue;
  4320. if (ri->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  4321. atomic_aggregate = 1;
  4322. if (ri->sub_type != BGP_ROUTE_AGGREGATE)
  4323. {
  4324. /* summary-only aggregate route suppress aggregated
  4325. route announcement. */
  4326. if (aggregate->summary_only)
  4327. {
  4328. (bgp_info_extra_get (ri))->suppress++;
  4329. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  4330. match++;
  4331. }
  4332. /* If at least one route among routes that are aggregated has
  4333. * ORIGIN with the value INCOMPLETE, then the aggregated route
  4334. * MUST have the ORIGIN attribute with the value INCOMPLETE.
  4335. * Otherwise, if at least one route among routes that are
  4336. * aggregated has ORIGIN with the value EGP, then the aggregated
  4337. * route MUST have the ORIGIN attribute with the value EGP.
  4338. */
  4339. if (origin < ri->attr->origin)
  4340. origin = ri->attr->origin;
  4341. /* as-set aggregate route generate origin, as path,
  4342. community aggregation. */
  4343. if (aggregate->as_set)
  4344. {
  4345. if (aspath)
  4346. {
  4347. asmerge = aspath_aggregate (aspath, ri->attr->aspath);
  4348. aspath_free (aspath);
  4349. aspath = asmerge;
  4350. }
  4351. else
  4352. aspath = aspath_dup (ri->attr->aspath);
  4353. if (ri->attr->community)
  4354. {
  4355. if (community)
  4356. {
  4357. commerge = community_merge (community,
  4358. ri->attr->community);
  4359. community = community_uniq_sort (commerge);
  4360. community_free (commerge);
  4361. }
  4362. else
  4363. community = community_dup (ri->attr->community);
  4364. }
  4365. }
  4366. aggregate->count++;
  4367. }
  4368. }
  4369. /* If this node is suppressed, process the change. */
  4370. if (match)
  4371. bgp_process (bgp, rn, afi, safi);
  4372. }
  4373. bgp_unlock_node (top);
  4374. /* Add aggregate route to BGP table. */
  4375. if (aggregate->count)
  4376. {
  4377. rn = bgp_node_get (table, p);
  4378. new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_AGGREGATE, bgp->peer_self,
  4379. bgp_attr_aggregate_intern(bgp, origin, aspath, community,
  4380. aggregate->as_set,
  4381. atomic_aggregate), rn);
  4382. SET_FLAG (new->flags, BGP_INFO_VALID);
  4383. bgp_info_add (rn, new);
  4384. bgp_unlock_node (rn);
  4385. /* Process change. */
  4386. bgp_process (bgp, rn, afi, safi);
  4387. }
  4388. else
  4389. {
  4390. if (aspath)
  4391. aspath_free (aspath);
  4392. if (community)
  4393. community_free (community);
  4394. }
  4395. }
  4396. void
  4397. bgp_aggregate_delete (struct bgp *bgp, struct prefix *p, afi_t afi,
  4398. safi_t safi, struct bgp_aggregate *aggregate)
  4399. {
  4400. struct bgp_table *table;
  4401. struct bgp_node *top;
  4402. struct bgp_node *rn;
  4403. struct bgp_info *ri;
  4404. unsigned long match;
  4405. table = bgp->rib[afi][safi];
  4406. if (afi == AFI_IP && p->prefixlen == IPV4_MAX_BITLEN)
  4407. return;
  4408. if (afi == AFI_IP6 && p->prefixlen == IPV6_MAX_BITLEN)
  4409. return;
  4410. /* If routes exists below this node, generate aggregate routes. */
  4411. top = bgp_node_get (table, p);
  4412. for (rn = bgp_node_get (table, p); rn; rn = bgp_route_next_until (rn, top))
  4413. if (rn->p.prefixlen > p->prefixlen)
  4414. {
  4415. match = 0;
  4416. for (ri = rn->info; ri; ri = ri->next)
  4417. {
  4418. if (BGP_INFO_HOLDDOWN (ri))
  4419. continue;
  4420. if (ri->sub_type != BGP_ROUTE_AGGREGATE)
  4421. {
  4422. if (aggregate->summary_only && ri->extra)
  4423. {
  4424. ri->extra->suppress--;
  4425. if (ri->extra->suppress == 0)
  4426. {
  4427. bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
  4428. match++;
  4429. }
  4430. }
  4431. aggregate->count--;
  4432. }
  4433. }
  4434. /* If this node was suppressed, process the change. */
  4435. if (match)
  4436. bgp_process (bgp, rn, afi, safi);
  4437. }
  4438. bgp_unlock_node (top);
  4439. /* Delete aggregate route from BGP table. */
  4440. rn = bgp_node_get (table, p);
  4441. for (ri = rn->info; ri; ri = ri->next)
  4442. if (ri->peer == bgp->peer_self
  4443. && ri->type == ZEBRA_ROUTE_BGP
  4444. && ri->sub_type == BGP_ROUTE_AGGREGATE)
  4445. break;
  4446. /* Withdraw static BGP route from routing table. */
  4447. if (ri)
  4448. {
  4449. bgp_info_delete (rn, ri);
  4450. bgp_process (bgp, rn, afi, safi);
  4451. }
  4452. /* Unlock bgp_node_lookup. */
  4453. bgp_unlock_node (rn);
  4454. }
  4455. /* Aggregate route attribute. */
  4456. #define AGGREGATE_SUMMARY_ONLY 1
  4457. #define AGGREGATE_AS_SET 1
  4458. static int
  4459. bgp_aggregate_unset (struct vty *vty, const char *prefix_str,
  4460. afi_t afi, safi_t safi)
  4461. {
  4462. int ret;
  4463. struct prefix p;
  4464. struct bgp_node *rn;
  4465. struct bgp *bgp;
  4466. struct bgp_aggregate *aggregate;
  4467. /* Convert string to prefix structure. */
  4468. ret = str2prefix (prefix_str, &p);
  4469. if (!ret)
  4470. {
  4471. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  4472. return CMD_WARNING;
  4473. }
  4474. apply_mask (&p);
  4475. /* Get BGP structure. */
  4476. bgp = vty->index;
  4477. /* Old configuration check. */
  4478. rn = bgp_node_lookup (bgp->aggregate[afi][safi], &p);
  4479. if (! rn)
  4480. {
  4481. vty_out (vty, "%% There is no aggregate-address configuration.%s",
  4482. VTY_NEWLINE);
  4483. return CMD_WARNING;
  4484. }
  4485. aggregate = rn->info;
  4486. if (aggregate->safi & SAFI_UNICAST)
  4487. bgp_aggregate_delete (bgp, &p, afi, SAFI_UNICAST, aggregate);
  4488. if (aggregate->safi & SAFI_MULTICAST)
  4489. bgp_aggregate_delete (bgp, &p, afi, SAFI_MULTICAST, aggregate);
  4490. /* Unlock aggregate address configuration. */
  4491. rn->info = NULL;
  4492. bgp_aggregate_free (aggregate);
  4493. bgp_unlock_node (rn);
  4494. bgp_unlock_node (rn);
  4495. return CMD_SUCCESS;
  4496. }
  4497. static int
  4498. bgp_aggregate_set (struct vty *vty, const char *prefix_str,
  4499. afi_t afi, safi_t safi,
  4500. u_char summary_only, u_char as_set)
  4501. {
  4502. int ret;
  4503. struct prefix p;
  4504. struct bgp_node *rn;
  4505. struct bgp *bgp;
  4506. struct bgp_aggregate *aggregate;
  4507. /* Convert string to prefix structure. */
  4508. ret = str2prefix (prefix_str, &p);
  4509. if (!ret)
  4510. {
  4511. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  4512. return CMD_WARNING;
  4513. }
  4514. apply_mask (&p);
  4515. /* Get BGP structure. */
  4516. bgp = vty->index;
  4517. /* Old configuration check. */
  4518. rn = bgp_node_get (bgp->aggregate[afi][safi], &p);
  4519. if (rn->info)
  4520. {
  4521. vty_out (vty, "There is already same aggregate network.%s", VTY_NEWLINE);
  4522. /* try to remove the old entry */
  4523. ret = bgp_aggregate_unset (vty, prefix_str, afi, safi);
  4524. if (ret)
  4525. {
  4526. vty_out (vty, "Error deleting aggregate.%s", VTY_NEWLINE);
  4527. bgp_unlock_node (rn);
  4528. return CMD_WARNING;
  4529. }
  4530. }
  4531. /* Make aggregate address structure. */
  4532. aggregate = bgp_aggregate_new ();
  4533. aggregate->summary_only = summary_only;
  4534. aggregate->as_set = as_set;
  4535. aggregate->safi = safi;
  4536. rn->info = aggregate;
  4537. /* Aggregate address insert into BGP routing table. */
  4538. if (safi & SAFI_UNICAST)
  4539. bgp_aggregate_add (bgp, &p, afi, SAFI_UNICAST, aggregate);
  4540. if (safi & SAFI_MULTICAST)
  4541. bgp_aggregate_add (bgp, &p, afi, SAFI_MULTICAST, aggregate);
  4542. return CMD_SUCCESS;
  4543. }
  4544. DEFUN (aggregate_address,
  4545. aggregate_address_cmd,
  4546. "aggregate-address A.B.C.D/M",
  4547. "Configure BGP aggregate entries\n"
  4548. "Aggregate prefix\n")
  4549. {
  4550. return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty), 0, 0);
  4551. }
  4552. DEFUN (aggregate_address_mask,
  4553. aggregate_address_mask_cmd,
  4554. "aggregate-address A.B.C.D A.B.C.D",
  4555. "Configure BGP aggregate entries\n"
  4556. "Aggregate address\n"
  4557. "Aggregate mask\n")
  4558. {
  4559. int ret;
  4560. char prefix_str[BUFSIZ];
  4561. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  4562. if (! ret)
  4563. {
  4564. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  4565. return CMD_WARNING;
  4566. }
  4567. return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
  4568. 0, 0);
  4569. }
  4570. DEFUN (aggregate_address_summary_only,
  4571. aggregate_address_summary_only_cmd,
  4572. "aggregate-address A.B.C.D/M summary-only",
  4573. "Configure BGP aggregate entries\n"
  4574. "Aggregate prefix\n"
  4575. "Filter more specific routes from updates\n")
  4576. {
  4577. return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty),
  4578. AGGREGATE_SUMMARY_ONLY, 0);
  4579. }
  4580. DEFUN (aggregate_address_mask_summary_only,
  4581. aggregate_address_mask_summary_only_cmd,
  4582. "aggregate-address A.B.C.D A.B.C.D summary-only",
  4583. "Configure BGP aggregate entries\n"
  4584. "Aggregate address\n"
  4585. "Aggregate mask\n"
  4586. "Filter more specific routes from updates\n")
  4587. {
  4588. int ret;
  4589. char prefix_str[BUFSIZ];
  4590. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  4591. if (! ret)
  4592. {
  4593. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  4594. return CMD_WARNING;
  4595. }
  4596. return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
  4597. AGGREGATE_SUMMARY_ONLY, 0);
  4598. }
  4599. DEFUN (aggregate_address_as_set,
  4600. aggregate_address_as_set_cmd,
  4601. "aggregate-address A.B.C.D/M as-set",
  4602. "Configure BGP aggregate entries\n"
  4603. "Aggregate prefix\n"
  4604. "Generate AS set path information\n")
  4605. {
  4606. return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty),
  4607. 0, AGGREGATE_AS_SET);
  4608. }
  4609. DEFUN (aggregate_address_mask_as_set,
  4610. aggregate_address_mask_as_set_cmd,
  4611. "aggregate-address A.B.C.D A.B.C.D as-set",
  4612. "Configure BGP aggregate entries\n"
  4613. "Aggregate address\n"
  4614. "Aggregate mask\n"
  4615. "Generate AS set path information\n")
  4616. {
  4617. int ret;
  4618. char prefix_str[BUFSIZ];
  4619. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  4620. if (! ret)
  4621. {
  4622. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  4623. return CMD_WARNING;
  4624. }
  4625. return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
  4626. 0, AGGREGATE_AS_SET);
  4627. }
  4628. DEFUN (aggregate_address_as_set_summary,
  4629. aggregate_address_as_set_summary_cmd,
  4630. "aggregate-address A.B.C.D/M as-set summary-only",
  4631. "Configure BGP aggregate entries\n"
  4632. "Aggregate prefix\n"
  4633. "Generate AS set path information\n"
  4634. "Filter more specific routes from updates\n")
  4635. {
  4636. return bgp_aggregate_set (vty, argv[0], AFI_IP, bgp_node_safi (vty),
  4637. AGGREGATE_SUMMARY_ONLY, AGGREGATE_AS_SET);
  4638. }
  4639. ALIAS (aggregate_address_as_set_summary,
  4640. aggregate_address_summary_as_set_cmd,
  4641. "aggregate-address A.B.C.D/M summary-only as-set",
  4642. "Configure BGP aggregate entries\n"
  4643. "Aggregate prefix\n"
  4644. "Filter more specific routes from updates\n"
  4645. "Generate AS set path information\n")
  4646. DEFUN (aggregate_address_mask_as_set_summary,
  4647. aggregate_address_mask_as_set_summary_cmd,
  4648. "aggregate-address A.B.C.D A.B.C.D as-set summary-only",
  4649. "Configure BGP aggregate entries\n"
  4650. "Aggregate address\n"
  4651. "Aggregate mask\n"
  4652. "Generate AS set path information\n"
  4653. "Filter more specific routes from updates\n")
  4654. {
  4655. int ret;
  4656. char prefix_str[BUFSIZ];
  4657. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  4658. if (! ret)
  4659. {
  4660. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  4661. return CMD_WARNING;
  4662. }
  4663. return bgp_aggregate_set (vty, prefix_str, AFI_IP, bgp_node_safi (vty),
  4664. AGGREGATE_SUMMARY_ONLY, AGGREGATE_AS_SET);
  4665. }
  4666. ALIAS (aggregate_address_mask_as_set_summary,
  4667. aggregate_address_mask_summary_as_set_cmd,
  4668. "aggregate-address A.B.C.D A.B.C.D summary-only as-set",
  4669. "Configure BGP aggregate entries\n"
  4670. "Aggregate address\n"
  4671. "Aggregate mask\n"
  4672. "Filter more specific routes from updates\n"
  4673. "Generate AS set path information\n")
  4674. DEFUN (no_aggregate_address,
  4675. no_aggregate_address_cmd,
  4676. "no aggregate-address A.B.C.D/M",
  4677. NO_STR
  4678. "Configure BGP aggregate entries\n"
  4679. "Aggregate prefix\n")
  4680. {
  4681. return bgp_aggregate_unset (vty, argv[0], AFI_IP, bgp_node_safi (vty));
  4682. }
  4683. ALIAS (no_aggregate_address,
  4684. no_aggregate_address_summary_only_cmd,
  4685. "no aggregate-address A.B.C.D/M summary-only",
  4686. NO_STR
  4687. "Configure BGP aggregate entries\n"
  4688. "Aggregate prefix\n"
  4689. "Filter more specific routes from updates\n")
  4690. ALIAS (no_aggregate_address,
  4691. no_aggregate_address_as_set_cmd,
  4692. "no aggregate-address A.B.C.D/M as-set",
  4693. NO_STR
  4694. "Configure BGP aggregate entries\n"
  4695. "Aggregate prefix\n"
  4696. "Generate AS set path information\n")
  4697. ALIAS (no_aggregate_address,
  4698. no_aggregate_address_as_set_summary_cmd,
  4699. "no aggregate-address A.B.C.D/M as-set summary-only",
  4700. NO_STR
  4701. "Configure BGP aggregate entries\n"
  4702. "Aggregate prefix\n"
  4703. "Generate AS set path information\n"
  4704. "Filter more specific routes from updates\n")
  4705. ALIAS (no_aggregate_address,
  4706. no_aggregate_address_summary_as_set_cmd,
  4707. "no aggregate-address A.B.C.D/M summary-only as-set",
  4708. NO_STR
  4709. "Configure BGP aggregate entries\n"
  4710. "Aggregate prefix\n"
  4711. "Filter more specific routes from updates\n"
  4712. "Generate AS set path information\n")
  4713. DEFUN (no_aggregate_address_mask,
  4714. no_aggregate_address_mask_cmd,
  4715. "no aggregate-address A.B.C.D A.B.C.D",
  4716. NO_STR
  4717. "Configure BGP aggregate entries\n"
  4718. "Aggregate address\n"
  4719. "Aggregate mask\n")
  4720. {
  4721. int ret;
  4722. char prefix_str[BUFSIZ];
  4723. ret = netmask_str2prefix_str (argv[0], argv[1], prefix_str);
  4724. if (! ret)
  4725. {
  4726. vty_out (vty, "%% Inconsistent address and mask%s", VTY_NEWLINE);
  4727. return CMD_WARNING;
  4728. }
  4729. return bgp_aggregate_unset (vty, prefix_str, AFI_IP, bgp_node_safi (vty));
  4730. }
  4731. ALIAS (no_aggregate_address_mask,
  4732. no_aggregate_address_mask_summary_only_cmd,
  4733. "no aggregate-address A.B.C.D A.B.C.D summary-only",
  4734. NO_STR
  4735. "Configure BGP aggregate entries\n"
  4736. "Aggregate address\n"
  4737. "Aggregate mask\n"
  4738. "Filter more specific routes from updates\n")
  4739. ALIAS (no_aggregate_address_mask,
  4740. no_aggregate_address_mask_as_set_cmd,
  4741. "no aggregate-address A.B.C.D A.B.C.D as-set",
  4742. NO_STR
  4743. "Configure BGP aggregate entries\n"
  4744. "Aggregate address\n"
  4745. "Aggregate mask\n"
  4746. "Generate AS set path information\n")
  4747. ALIAS (no_aggregate_address_mask,
  4748. no_aggregate_address_mask_as_set_summary_cmd,
  4749. "no aggregate-address A.B.C.D A.B.C.D as-set summary-only",
  4750. NO_STR
  4751. "Configure BGP aggregate entries\n"
  4752. "Aggregate address\n"
  4753. "Aggregate mask\n"
  4754. "Generate AS set path information\n"
  4755. "Filter more specific routes from updates\n")
  4756. ALIAS (no_aggregate_address_mask,
  4757. no_aggregate_address_mask_summary_as_set_cmd,
  4758. "no aggregate-address A.B.C.D A.B.C.D summary-only as-set",
  4759. NO_STR
  4760. "Configure BGP aggregate entries\n"
  4761. "Aggregate address\n"
  4762. "Aggregate mask\n"
  4763. "Filter more specific routes from updates\n"
  4764. "Generate AS set path information\n")
  4765. DEFUN (ipv6_aggregate_address,
  4766. ipv6_aggregate_address_cmd,
  4767. "aggregate-address X:X::X:X/M",
  4768. "Configure BGP aggregate entries\n"
  4769. "Aggregate prefix\n")
  4770. {
  4771. return bgp_aggregate_set (vty, argv[0], AFI_IP6, SAFI_UNICAST, 0, 0);
  4772. }
  4773. DEFUN (ipv6_aggregate_address_summary_only,
  4774. ipv6_aggregate_address_summary_only_cmd,
  4775. "aggregate-address X:X::X:X/M summary-only",
  4776. "Configure BGP aggregate entries\n"
  4777. "Aggregate prefix\n"
  4778. "Filter more specific routes from updates\n")
  4779. {
  4780. return bgp_aggregate_set (vty, argv[0], AFI_IP6, SAFI_UNICAST,
  4781. AGGREGATE_SUMMARY_ONLY, 0);
  4782. }
  4783. DEFUN (no_ipv6_aggregate_address,
  4784. no_ipv6_aggregate_address_cmd,
  4785. "no aggregate-address X:X::X:X/M",
  4786. NO_STR
  4787. "Configure BGP aggregate entries\n"
  4788. "Aggregate prefix\n")
  4789. {
  4790. return bgp_aggregate_unset (vty, argv[0], AFI_IP6, SAFI_UNICAST);
  4791. }
  4792. DEFUN (no_ipv6_aggregate_address_summary_only,
  4793. no_ipv6_aggregate_address_summary_only_cmd,
  4794. "no aggregate-address X:X::X:X/M summary-only",
  4795. NO_STR
  4796. "Configure BGP aggregate entries\n"
  4797. "Aggregate prefix\n"
  4798. "Filter more specific routes from updates\n")
  4799. {
  4800. return bgp_aggregate_unset (vty, argv[0], AFI_IP6, SAFI_UNICAST);
  4801. }
  4802. ALIAS (ipv6_aggregate_address,
  4803. old_ipv6_aggregate_address_cmd,
  4804. "ipv6 bgp aggregate-address X:X::X:X/M",
  4805. IPV6_STR
  4806. BGP_STR
  4807. "Configure BGP aggregate entries\n"
  4808. "Aggregate prefix\n")
  4809. ALIAS (ipv6_aggregate_address_summary_only,
  4810. old_ipv6_aggregate_address_summary_only_cmd,
  4811. "ipv6 bgp aggregate-address X:X::X:X/M summary-only",
  4812. IPV6_STR
  4813. BGP_STR
  4814. "Configure BGP aggregate entries\n"
  4815. "Aggregate prefix\n"
  4816. "Filter more specific routes from updates\n")
  4817. ALIAS (no_ipv6_aggregate_address,
  4818. old_no_ipv6_aggregate_address_cmd,
  4819. "no ipv6 bgp aggregate-address X:X::X:X/M",
  4820. NO_STR
  4821. IPV6_STR
  4822. BGP_STR
  4823. "Configure BGP aggregate entries\n"
  4824. "Aggregate prefix\n")
  4825. ALIAS (no_ipv6_aggregate_address_summary_only,
  4826. old_no_ipv6_aggregate_address_summary_only_cmd,
  4827. "no ipv6 bgp aggregate-address X:X::X:X/M summary-only",
  4828. NO_STR
  4829. IPV6_STR
  4830. BGP_STR
  4831. "Configure BGP aggregate entries\n"
  4832. "Aggregate prefix\n"
  4833. "Filter more specific routes from updates\n")
  4834. /* Redistribute route treatment. */
  4835. void
  4836. bgp_redistribute_add (struct prefix *p, const struct in_addr *nexthop,
  4837. const struct in6_addr *nexthop6,
  4838. u_int32_t metric, u_char type, route_tag_t tag)
  4839. {
  4840. struct bgp *bgp;
  4841. struct listnode *node, *nnode;
  4842. struct bgp_info *new;
  4843. struct bgp_info *bi;
  4844. struct bgp_info info;
  4845. struct bgp_node *bn;
  4846. struct attr attr;
  4847. struct attr *new_attr;
  4848. afi_t afi;
  4849. int ret;
  4850. /* Make default attribute. */
  4851. bgp_attr_default_set (&attr, BGP_ORIGIN_INCOMPLETE);
  4852. if (nexthop)
  4853. attr.nexthop = *nexthop;
  4854. if (nexthop6)
  4855. {
  4856. struct attr_extra *extra = bgp_attr_extra_get(&attr);
  4857. extra->mp_nexthop_global = *nexthop6;
  4858. extra->mp_nexthop_len = 16;
  4859. }
  4860. attr.med = metric;
  4861. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
  4862. attr.extra->tag = tag;
  4863. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  4864. {
  4865. afi = family2afi (p->family);
  4866. if (bgp->redist[afi][type])
  4867. {
  4868. struct attr attr_new;
  4869. struct attr_extra extra_new;
  4870. /* Copy attribute for modification. */
  4871. attr_new.extra = &extra_new;
  4872. bgp_attr_dup (&attr_new, &attr);
  4873. if (bgp->redist_metric_flag[afi][type])
  4874. attr_new.med = bgp->redist_metric[afi][type];
  4875. /* Apply route-map. */
  4876. if (bgp->rmap[afi][type].name)
  4877. {
  4878. info.peer = bgp->peer_self;
  4879. info.attr = &attr_new;
  4880. SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_REDISTRIBUTE);
  4881. ret = route_map_apply (bgp->rmap[afi][type].map, p, RMAP_BGP,
  4882. &info);
  4883. bgp->peer_self->rmap_type = 0;
  4884. if (ret == RMAP_DENYMATCH)
  4885. {
  4886. /* Free uninterned attribute. */
  4887. bgp_attr_flush (&attr_new);
  4888. /* Unintern original. */
  4889. aspath_unintern (&attr.aspath);
  4890. bgp_attr_extra_free (&attr);
  4891. bgp_redistribute_delete (p, type);
  4892. return;
  4893. }
  4894. }
  4895. bn = bgp_afi_node_get (bgp->rib[afi][SAFI_UNICAST],
  4896. afi, SAFI_UNICAST, p, NULL);
  4897. new_attr = bgp_attr_intern (&attr_new);
  4898. for (bi = bn->info; bi; bi = bi->next)
  4899. if (bi->peer == bgp->peer_self
  4900. && bi->sub_type == BGP_ROUTE_REDISTRIBUTE)
  4901. break;
  4902. if (bi)
  4903. {
  4904. if (attrhash_cmp (bi->attr, new_attr) &&
  4905. !CHECK_FLAG(bi->flags, BGP_INFO_REMOVED))
  4906. {
  4907. bgp_attr_unintern (&new_attr);
  4908. aspath_unintern (&attr.aspath);
  4909. bgp_attr_extra_free (&attr);
  4910. bgp_unlock_node (bn);
  4911. return;
  4912. }
  4913. else
  4914. {
  4915. /* The attribute is changed. */
  4916. bgp_info_set_flag (bn, bi, BGP_INFO_ATTR_CHANGED);
  4917. /* Rewrite BGP route information. */
  4918. if (CHECK_FLAG(bi->flags, BGP_INFO_REMOVED))
  4919. bgp_info_restore(bn, bi);
  4920. else
  4921. bgp_aggregate_decrement (bgp, p, bi, afi, SAFI_UNICAST);
  4922. bgp_attr_unintern (&bi->attr);
  4923. bi->attr = new_attr;
  4924. bi->uptime = bgp_clock ();
  4925. /* Process change. */
  4926. bgp_aggregate_increment (bgp, p, bi, afi, SAFI_UNICAST);
  4927. bgp_process (bgp, bn, afi, SAFI_UNICAST);
  4928. bgp_unlock_node (bn);
  4929. aspath_unintern (&attr.aspath);
  4930. bgp_attr_extra_free (&attr);
  4931. return;
  4932. }
  4933. }
  4934. new = info_make(type, BGP_ROUTE_REDISTRIBUTE, bgp->peer_self,
  4935. new_attr, bn);
  4936. SET_FLAG (new->flags, BGP_INFO_VALID);
  4937. bgp_aggregate_increment (bgp, p, new, afi, SAFI_UNICAST);
  4938. bgp_info_add (bn, new);
  4939. bgp_unlock_node (bn);
  4940. bgp_process (bgp, bn, afi, SAFI_UNICAST);
  4941. }
  4942. }
  4943. /* Unintern original. */
  4944. aspath_unintern (&attr.aspath);
  4945. bgp_attr_extra_free (&attr);
  4946. }
  4947. void
  4948. bgp_redistribute_delete (struct prefix *p, u_char type)
  4949. {
  4950. struct bgp *bgp;
  4951. struct listnode *node, *nnode;
  4952. afi_t afi;
  4953. struct bgp_node *rn;
  4954. struct bgp_info *ri;
  4955. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  4956. {
  4957. afi = family2afi (p->family);
  4958. if (bgp->redist[afi][type])
  4959. {
  4960. rn = bgp_afi_node_get (bgp->rib[afi][SAFI_UNICAST], afi, SAFI_UNICAST, p, NULL);
  4961. for (ri = rn->info; ri; ri = ri->next)
  4962. if (ri->peer == bgp->peer_self
  4963. && ri->type == type)
  4964. break;
  4965. if (ri)
  4966. {
  4967. bgp_aggregate_decrement (bgp, p, ri, afi, SAFI_UNICAST);
  4968. bgp_info_delete (rn, ri);
  4969. bgp_process (bgp, rn, afi, SAFI_UNICAST);
  4970. }
  4971. bgp_unlock_node (rn);
  4972. }
  4973. }
  4974. }
  4975. /* Withdraw specified route type's route. */
  4976. void
  4977. bgp_redistribute_withdraw (struct bgp *bgp, afi_t afi, int type)
  4978. {
  4979. struct bgp_node *rn;
  4980. struct bgp_info *ri;
  4981. struct bgp_table *table;
  4982. table = bgp->rib[afi][SAFI_UNICAST];
  4983. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  4984. {
  4985. for (ri = rn->info; ri; ri = ri->next)
  4986. if (ri->peer == bgp->peer_self
  4987. && ri->type == type)
  4988. break;
  4989. if (ri)
  4990. {
  4991. bgp_aggregate_decrement (bgp, &rn->p, ri, afi, SAFI_UNICAST);
  4992. bgp_info_delete (rn, ri);
  4993. bgp_process (bgp, rn, afi, SAFI_UNICAST);
  4994. }
  4995. }
  4996. }
  4997. /* Static function to display route. */
  4998. static void
  4999. route_vty_out_route (struct prefix *p, struct vty *vty)
  5000. {
  5001. int len;
  5002. u_int32_t destination;
  5003. char buf[BUFSIZ];
  5004. if (p->family == AF_INET)
  5005. {
  5006. len = vty_out (vty, "%s", inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ));
  5007. destination = ntohl (p->u.prefix4.s_addr);
  5008. if ((IN_CLASSC (destination) && p->prefixlen == 24)
  5009. || (IN_CLASSB (destination) && p->prefixlen == 16)
  5010. || (IN_CLASSA (destination) && p->prefixlen == 8)
  5011. || p->u.prefix4.s_addr == 0)
  5012. {
  5013. /* When mask is natural, mask is not displayed. */
  5014. }
  5015. else
  5016. len += vty_out (vty, "/%d", p->prefixlen);
  5017. }
  5018. else
  5019. len = vty_out (vty, "%s/%d", inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
  5020. p->prefixlen);
  5021. len = 17 - len;
  5022. if (len < 1)
  5023. vty_out (vty, "%s%*s", VTY_NEWLINE, 20, " ");
  5024. else
  5025. vty_out (vty, "%*s", len, " ");
  5026. }
  5027. enum bgp_display_type
  5028. {
  5029. normal_list,
  5030. };
  5031. /* Print the short form route status for a bgp_info */
  5032. static void
  5033. route_vty_short_status_out (struct vty *vty, struct bgp_info *binfo)
  5034. {
  5035. /* Route status display. */
  5036. if (CHECK_FLAG (binfo->flags, BGP_INFO_REMOVED))
  5037. vty_out (vty, "R");
  5038. else if (CHECK_FLAG (binfo->flags, BGP_INFO_STALE))
  5039. vty_out (vty, "S");
  5040. else if (binfo->extra && binfo->extra->suppress)
  5041. vty_out (vty, "s");
  5042. else if (CHECK_FLAG (binfo->flags, BGP_INFO_VALID) &&
  5043. ! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
  5044. vty_out (vty, "*");
  5045. else
  5046. vty_out (vty, " ");
  5047. /* Selected */
  5048. if (CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
  5049. vty_out (vty, "h");
  5050. else if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED))
  5051. vty_out (vty, "d");
  5052. else if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
  5053. vty_out (vty, ">");
  5054. else if (CHECK_FLAG (binfo->flags, BGP_INFO_MULTIPATH))
  5055. vty_out (vty, "=");
  5056. else
  5057. vty_out (vty, " ");
  5058. /* Internal route. */
  5059. if ((binfo->peer->as) && (binfo->peer->as == binfo->peer->local_as))
  5060. vty_out (vty, "i");
  5061. else
  5062. vty_out (vty, " ");
  5063. }
  5064. /* called from terminal list command */
  5065. void
  5066. route_vty_out(
  5067. struct vty *vty,
  5068. struct prefix *p,
  5069. struct bgp_info *binfo,
  5070. int display,
  5071. safi_t safi)
  5072. {
  5073. struct attr *attr;
  5074. /* short status lead text */
  5075. route_vty_short_status_out (vty, binfo);
  5076. /* print prefix and mask */
  5077. if (!display)
  5078. route_vty_out_route (p, vty);
  5079. else
  5080. vty_out (vty, "%*s", 17, " ");
  5081. /* Print attribute */
  5082. attr = binfo->attr;
  5083. if (attr)
  5084. {
  5085. /*
  5086. * NEXTHOP start
  5087. */
  5088. /*
  5089. * For ENCAP routes, nexthop address family is not
  5090. * neccessarily the same as the prefix address family.
  5091. * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
  5092. */
  5093. if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) {
  5094. if (attr->extra) {
  5095. char buf[BUFSIZ];
  5096. int af = NEXTHOP_FAMILY(attr->extra->mp_nexthop_len);
  5097. switch (af) {
  5098. case AF_INET:
  5099. vty_out (vty, "%s", inet_ntop(af,
  5100. &attr->extra->mp_nexthop_global_in, buf, BUFSIZ));
  5101. break;
  5102. case AF_INET6:
  5103. vty_out (vty, "%s", inet_ntop(af,
  5104. &attr->extra->mp_nexthop_global, buf, BUFSIZ));
  5105. break;
  5106. default:
  5107. vty_out(vty, "?");
  5108. }
  5109. } else {
  5110. vty_out(vty, "?");
  5111. }
  5112. } else {
  5113. if (p->family == AF_INET)
  5114. {
  5115. vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
  5116. }
  5117. else if (p->family == AF_INET6)
  5118. {
  5119. int len;
  5120. char buf[BUFSIZ];
  5121. len = vty_out (vty, "%s",
  5122. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
  5123. buf, BUFSIZ));
  5124. len = 16 - len;
  5125. if (len < 1)
  5126. vty_out (vty, "%s%*s", VTY_NEWLINE, 36, " ");
  5127. else
  5128. vty_out (vty, "%*s", len, " ");
  5129. }
  5130. else
  5131. {
  5132. vty_out(vty, "?");
  5133. }
  5134. }
  5135. /*
  5136. * NEXTHOP end
  5137. */
  5138. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  5139. vty_out (vty, "%10u ", attr->med);
  5140. else
  5141. vty_out (vty, " ");
  5142. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
  5143. vty_out (vty, "%7u ", attr->local_pref);
  5144. else
  5145. vty_out (vty, " ");
  5146. vty_out (vty, "%7u ", (attr->extra ? attr->extra->weight : 0));
  5147. /* Print aspath */
  5148. if (attr->aspath)
  5149. aspath_print_vty (vty, "%s", attr->aspath, " ");
  5150. /* Print origin */
  5151. vty_out (vty, "%s", bgp_origin_str[attr->origin]);
  5152. }
  5153. vty_out (vty, "%s", VTY_NEWLINE);
  5154. }
  5155. /* called from terminal list command */
  5156. void
  5157. route_vty_out_tmp (struct vty *vty, struct prefix *p,
  5158. struct attr *attr, safi_t safi)
  5159. {
  5160. /* Route status display. */
  5161. vty_out (vty, "*");
  5162. vty_out (vty, ">");
  5163. vty_out (vty, " ");
  5164. /* print prefix and mask */
  5165. route_vty_out_route (p, vty);
  5166. /* Print attribute */
  5167. if (attr)
  5168. {
  5169. if (p->family == AF_INET)
  5170. {
  5171. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  5172. vty_out (vty, "%-16s",
  5173. inet_ntoa (attr->extra->mp_nexthop_global_in));
  5174. else
  5175. vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
  5176. }
  5177. else if (p->family == AF_INET6)
  5178. {
  5179. int len;
  5180. char buf[BUFSIZ];
  5181. assert (attr->extra);
  5182. len = vty_out (vty, "%s",
  5183. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
  5184. buf, BUFSIZ));
  5185. len = 16 - len;
  5186. if (len < 1)
  5187. vty_out (vty, "%s%*s", VTY_NEWLINE, 36, " ");
  5188. else
  5189. vty_out (vty, "%*s", len, " ");
  5190. }
  5191. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  5192. vty_out (vty, "%10u ", attr->med);
  5193. else
  5194. vty_out (vty, " ");
  5195. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
  5196. vty_out (vty, "%7u ", attr->local_pref);
  5197. else
  5198. vty_out (vty, " ");
  5199. vty_out (vty, "%7u ", (attr->extra ? attr->extra->weight : 0));
  5200. /* Print aspath */
  5201. if (attr->aspath)
  5202. aspath_print_vty (vty, "%s", attr->aspath, " ");
  5203. /* Print origin */
  5204. vty_out (vty, "%s", bgp_origin_str[attr->origin]);
  5205. }
  5206. vty_out (vty, "%s", VTY_NEWLINE);
  5207. }
  5208. void
  5209. route_vty_out_tag (struct vty *vty, struct prefix *p,
  5210. struct bgp_info *binfo, int display, safi_t safi)
  5211. {
  5212. struct attr *attr;
  5213. u_int32_t label = 0;
  5214. if (!binfo->extra)
  5215. return;
  5216. /* short status lead text */
  5217. route_vty_short_status_out (vty, binfo);
  5218. /* print prefix and mask */
  5219. if (! display)
  5220. route_vty_out_route (p, vty);
  5221. else
  5222. vty_out (vty, "%*s", 17, " ");
  5223. /* Print attribute */
  5224. attr = binfo->attr;
  5225. if (attr)
  5226. {
  5227. if (p->family == AF_INET)
  5228. {
  5229. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  5230. vty_out (vty, "%-16s",
  5231. inet_ntoa (attr->extra->mp_nexthop_global_in));
  5232. else
  5233. vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
  5234. }
  5235. else if (p->family == AF_INET6)
  5236. {
  5237. assert (attr->extra);
  5238. char buf[BUFSIZ];
  5239. char buf1[BUFSIZ];
  5240. if (attr->extra->mp_nexthop_len == 16)
  5241. vty_out (vty, "%s",
  5242. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
  5243. buf, BUFSIZ));
  5244. else if (attr->extra->mp_nexthop_len == 32)
  5245. vty_out (vty, "%s(%s)",
  5246. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
  5247. buf, BUFSIZ),
  5248. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_local,
  5249. buf1, BUFSIZ));
  5250. }
  5251. }
  5252. label = decode_label (binfo->extra->tag);
  5253. vty_out (vty, "notag/%d", label);
  5254. vty_out (vty, "%s", VTY_NEWLINE);
  5255. }
  5256. /* dampening route */
  5257. static void
  5258. damp_route_vty_out (struct vty *vty, struct prefix *p,
  5259. struct bgp_info *binfo, int display, safi_t safi)
  5260. {
  5261. struct attr *attr;
  5262. int len;
  5263. char timebuf[BGP_UPTIME_LEN];
  5264. /* short status lead text */
  5265. route_vty_short_status_out (vty, binfo);
  5266. /* print prefix and mask */
  5267. if (! display)
  5268. route_vty_out_route (p, vty);
  5269. else
  5270. vty_out (vty, "%*s", 17, " ");
  5271. len = vty_out (vty, "%s", binfo->peer->host);
  5272. len = 17 - len;
  5273. if (len < 1)
  5274. vty_out (vty, "%s%*s", VTY_NEWLINE, 34, " ");
  5275. else
  5276. vty_out (vty, "%*s", len, " ");
  5277. vty_out (vty, "%s ", bgp_damp_reuse_time_vty (vty, binfo, timebuf, BGP_UPTIME_LEN));
  5278. /* Print attribute */
  5279. attr = binfo->attr;
  5280. if (attr)
  5281. {
  5282. /* Print aspath */
  5283. if (attr->aspath)
  5284. aspath_print_vty (vty, "%s", attr->aspath, " ");
  5285. /* Print origin */
  5286. vty_out (vty, "%s", bgp_origin_str[attr->origin]);
  5287. }
  5288. vty_out (vty, "%s", VTY_NEWLINE);
  5289. }
  5290. /* flap route */
  5291. static void
  5292. flap_route_vty_out (struct vty *vty, struct prefix *p,
  5293. struct bgp_info *binfo, int display, safi_t safi)
  5294. {
  5295. struct attr *attr;
  5296. struct bgp_damp_info *bdi;
  5297. char timebuf[BGP_UPTIME_LEN];
  5298. int len;
  5299. if (!binfo->extra)
  5300. return;
  5301. bdi = binfo->extra->damp_info;
  5302. /* short status lead text */
  5303. route_vty_short_status_out (vty, binfo);
  5304. /* print prefix and mask */
  5305. if (! display)
  5306. route_vty_out_route (p, vty);
  5307. else
  5308. vty_out (vty, "%*s", 17, " ");
  5309. len = vty_out (vty, "%s", binfo->peer->host);
  5310. len = 16 - len;
  5311. if (len < 1)
  5312. vty_out (vty, "%s%*s", VTY_NEWLINE, 33, " ");
  5313. else
  5314. vty_out (vty, "%*s", len, " ");
  5315. len = vty_out (vty, "%d", bdi->flap);
  5316. len = 5 - len;
  5317. if (len < 1)
  5318. vty_out (vty, " ");
  5319. else
  5320. vty_out (vty, "%*s ", len, " ");
  5321. vty_out (vty, "%s ", peer_uptime (bdi->start_time,
  5322. timebuf, BGP_UPTIME_LEN));
  5323. if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED)
  5324. && ! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
  5325. vty_out (vty, "%s ", bgp_damp_reuse_time_vty (vty, binfo, timebuf, BGP_UPTIME_LEN));
  5326. else
  5327. vty_out (vty, "%*s ", 8, " ");
  5328. /* Print attribute */
  5329. attr = binfo->attr;
  5330. if (attr)
  5331. {
  5332. /* Print aspath */
  5333. if (attr->aspath)
  5334. aspath_print_vty (vty, "%s", attr->aspath, " ");
  5335. /* Print origin */
  5336. vty_out (vty, "%s", bgp_origin_str[attr->origin]);
  5337. }
  5338. vty_out (vty, "%s", VTY_NEWLINE);
  5339. }
  5340. static void
  5341. route_vty_out_detail (struct vty *vty, struct bgp *bgp, struct prefix *p,
  5342. struct bgp_info *binfo, afi_t afi, safi_t safi)
  5343. {
  5344. char buf[INET6_ADDRSTRLEN];
  5345. char buf1[BUFSIZ];
  5346. struct attr *attr;
  5347. int sockunion_vty_out (struct vty *, union sockunion *);
  5348. #ifdef HAVE_CLOCK_MONOTONIC
  5349. time_t tbuf;
  5350. #endif
  5351. attr = binfo->attr;
  5352. if (attr)
  5353. {
  5354. /* Line1 display AS-path, Aggregator */
  5355. if (attr->aspath)
  5356. {
  5357. vty_out (vty, " ");
  5358. if (aspath_count_hops (attr->aspath) == 0)
  5359. vty_out (vty, "Local");
  5360. else
  5361. aspath_print_vty (vty, "%s", attr->aspath, "");
  5362. }
  5363. if (CHECK_FLAG (binfo->flags, BGP_INFO_REMOVED))
  5364. vty_out (vty, ", (removed)");
  5365. if (CHECK_FLAG (binfo->flags, BGP_INFO_STALE))
  5366. vty_out (vty, ", (stale)");
  5367. if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)))
  5368. vty_out (vty, ", (aggregated by %u %s)",
  5369. attr->extra->aggregator_as,
  5370. inet_ntoa (attr->extra->aggregator_addr));
  5371. if (CHECK_FLAG (binfo->peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
  5372. vty_out (vty, ", (Received from a RR-client)");
  5373. if (CHECK_FLAG (binfo->peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  5374. vty_out (vty, ", (Received from a RS-client)");
  5375. if (CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
  5376. vty_out (vty, ", (history entry)");
  5377. else if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED))
  5378. vty_out (vty, ", (suppressed due to dampening)");
  5379. vty_out (vty, "%s", VTY_NEWLINE);
  5380. /* Line2 display Next-hop, Neighbor, Router-id */
  5381. if (p->family == AF_INET)
  5382. {
  5383. vty_out (vty, " %s", ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)) ?
  5384. inet_ntoa (attr->extra->mp_nexthop_global_in) :
  5385. inet_ntoa (attr->nexthop));
  5386. }
  5387. else
  5388. {
  5389. assert (attr->extra);
  5390. vty_out (vty, " %s",
  5391. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
  5392. buf, INET6_ADDRSTRLEN));
  5393. }
  5394. if (binfo->peer == bgp->peer_self)
  5395. {
  5396. vty_out (vty, " from %s ",
  5397. p->family == AF_INET ? "0.0.0.0" : "::");
  5398. vty_out (vty, "(%s)", inet_ntoa(bgp->router_id));
  5399. }
  5400. else
  5401. {
  5402. if (! CHECK_FLAG (binfo->flags, BGP_INFO_VALID))
  5403. vty_out (vty, " (inaccessible)");
  5404. else if (binfo->extra && binfo->extra->igpmetric)
  5405. vty_out (vty, " (metric %u)", binfo->extra->igpmetric);
  5406. if (!sockunion2str (&binfo->peer->su, buf, sizeof(buf))) {
  5407. buf[0] = '?';
  5408. buf[1] = 0;
  5409. }
  5410. vty_out (vty, " from %s", buf);
  5411. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
  5412. vty_out (vty, " (%s)", inet_ntoa (attr->extra->originator_id));
  5413. else
  5414. vty_out (vty, " (%s)", inet_ntop (AF_INET, &binfo->peer->remote_id, buf1, BUFSIZ));
  5415. }
  5416. vty_out (vty, "%s", VTY_NEWLINE);
  5417. /* display nexthop local */
  5418. if (attr->extra && attr->extra->mp_nexthop_len == 32)
  5419. {
  5420. vty_out (vty, " (%s)%s",
  5421. inet_ntop (AF_INET6, &attr->extra->mp_nexthop_local,
  5422. buf, INET6_ADDRSTRLEN),
  5423. VTY_NEWLINE);
  5424. }
  5425. /* Line 3 display Origin, Med, Locpref, Weight, Tag, valid, Int/Ext/Local, Atomic, best */
  5426. vty_out (vty, " Origin %s", bgp_origin_long_str[attr->origin]);
  5427. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
  5428. vty_out (vty, ", metric %u", attr->med);
  5429. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
  5430. vty_out (vty, ", localpref %u", attr->local_pref);
  5431. else
  5432. vty_out (vty, ", localpref %u", bgp->default_local_pref);
  5433. if (attr->extra && attr->extra->weight != 0)
  5434. vty_out (vty, ", weight %u", attr->extra->weight);
  5435. if (attr->extra && attr->extra->tag != 0)
  5436. vty_out (vty, ", tag %d", attr->extra->tag);
  5437. if (! CHECK_FLAG (binfo->flags, BGP_INFO_VALID))
  5438. vty_out (vty, ", invalid");
  5439. else if (! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY))
  5440. vty_out (vty, ", valid");
  5441. if (binfo->peer != bgp->peer_self)
  5442. {
  5443. if (binfo->peer->as == binfo->peer->local_as)
  5444. vty_out (vty, ", internal");
  5445. else
  5446. vty_out (vty, ", %s",
  5447. (bgp_confederation_peers_check(bgp, binfo->peer->as) ? "confed-external" : "external"));
  5448. }
  5449. else if (binfo->sub_type == BGP_ROUTE_AGGREGATE)
  5450. vty_out (vty, ", aggregated, local");
  5451. else if (binfo->type != ZEBRA_ROUTE_BGP)
  5452. vty_out (vty, ", sourced");
  5453. else
  5454. vty_out (vty, ", sourced, local");
  5455. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
  5456. vty_out (vty, ", atomic-aggregate");
  5457. if (CHECK_FLAG (binfo->flags, BGP_INFO_MULTIPATH) ||
  5458. (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED) &&
  5459. bgp_info_mpath_count (binfo)))
  5460. vty_out (vty, ", multipath");
  5461. if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
  5462. vty_out (vty, ", best");
  5463. vty_out (vty, "%s", VTY_NEWLINE);
  5464. /* Line 4 display Community */
  5465. if (attr->community)
  5466. vty_out (vty, " Community: %s%s", attr->community->str,
  5467. VTY_NEWLINE);
  5468. /* Line 5 display Extended-community */
  5469. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))
  5470. vty_out (vty, " Extended Community: %s%s",
  5471. attr->extra->ecommunity->str, VTY_NEWLINE);
  5472. /* Line 6 display Originator, Cluster-id */
  5473. if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) ||
  5474. (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)))
  5475. {
  5476. assert (attr->extra);
  5477. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
  5478. vty_out (vty, " Originator: %s",
  5479. inet_ntoa (attr->extra->originator_id));
  5480. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))
  5481. {
  5482. int i;
  5483. vty_out (vty, ", Cluster list: ");
  5484. for (i = 0; i < attr->extra->cluster->length / 4; i++)
  5485. vty_out (vty, "%s ",
  5486. inet_ntoa (attr->extra->cluster->list[i]));
  5487. }
  5488. vty_out (vty, "%s", VTY_NEWLINE);
  5489. }
  5490. if (binfo->extra && binfo->extra->damp_info)
  5491. bgp_damp_info_vty (vty, binfo);
  5492. /* Line 7 display Uptime */
  5493. #ifdef HAVE_CLOCK_MONOTONIC
  5494. tbuf = time(NULL) - (bgp_clock() - binfo->uptime);
  5495. vty_out (vty, " Last update: %s", ctime(&tbuf));
  5496. #else
  5497. vty_out (vty, " Last update: %s", ctime(&binfo->uptime));
  5498. #endif /* HAVE_CLOCK_MONOTONIC */
  5499. }
  5500. vty_out (vty, "%s", VTY_NEWLINE);
  5501. }
  5502. #define BGP_SHOW_SCODE_HEADER "Status codes: s suppressed, d damped, "\
  5503. "h history, * valid, > best, = multipath,%s"\
  5504. " i internal, r RIB-failure, S Stale, R Removed%s"
  5505. #define BGP_SHOW_OCODE_HEADER "Origin codes: i - IGP, e - EGP, ? - incomplete%s%s"
  5506. #define BGP_SHOW_HEADER " Network Next Hop Metric LocPrf Weight Path%s"
  5507. #define BGP_SHOW_DAMP_HEADER " Network From Reuse Path%s"
  5508. #define BGP_SHOW_FLAP_HEADER " Network From Flaps Duration Reuse Path%s"
  5509. enum bgp_show_type
  5510. {
  5511. bgp_show_type_normal,
  5512. bgp_show_type_regexp,
  5513. bgp_show_type_prefix_list,
  5514. bgp_show_type_filter_list,
  5515. bgp_show_type_route_map,
  5516. bgp_show_type_neighbor,
  5517. bgp_show_type_cidr_only,
  5518. bgp_show_type_prefix_longer,
  5519. bgp_show_type_community_all,
  5520. bgp_show_type_community,
  5521. bgp_show_type_community_exact,
  5522. bgp_show_type_community_list,
  5523. bgp_show_type_community_list_exact,
  5524. bgp_show_type_flap_statistics,
  5525. bgp_show_type_flap_address,
  5526. bgp_show_type_flap_prefix,
  5527. bgp_show_type_flap_cidr_only,
  5528. bgp_show_type_flap_regexp,
  5529. bgp_show_type_flap_filter_list,
  5530. bgp_show_type_flap_prefix_list,
  5531. bgp_show_type_flap_prefix_longer,
  5532. bgp_show_type_flap_route_map,
  5533. bgp_show_type_flap_neighbor,
  5534. bgp_show_type_dampend_paths,
  5535. bgp_show_type_damp_neighbor
  5536. };
  5537. static int
  5538. bgp_show_table (struct vty *vty, struct bgp_table *table, struct in_addr *router_id,
  5539. enum bgp_show_type type, void *output_arg)
  5540. {
  5541. struct bgp_info *ri;
  5542. struct bgp_node *rn;
  5543. int header = 1;
  5544. int display;
  5545. unsigned long output_count;
  5546. unsigned long total_count;
  5547. /* This is first entry point, so reset total line. */
  5548. output_count = 0;
  5549. total_count = 0;
  5550. /* Start processing of routes. */
  5551. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  5552. if (rn->info != NULL)
  5553. {
  5554. display = 0;
  5555. for (ri = rn->info; ri; ri = ri->next)
  5556. {
  5557. total_count++;
  5558. if (type == bgp_show_type_flap_statistics
  5559. || type == bgp_show_type_flap_address
  5560. || type == bgp_show_type_flap_prefix
  5561. || type == bgp_show_type_flap_cidr_only
  5562. || type == bgp_show_type_flap_regexp
  5563. || type == bgp_show_type_flap_filter_list
  5564. || type == bgp_show_type_flap_prefix_list
  5565. || type == bgp_show_type_flap_prefix_longer
  5566. || type == bgp_show_type_flap_route_map
  5567. || type == bgp_show_type_flap_neighbor
  5568. || type == bgp_show_type_dampend_paths
  5569. || type == bgp_show_type_damp_neighbor)
  5570. {
  5571. if (!(ri->extra && ri->extra->damp_info))
  5572. continue;
  5573. }
  5574. if (type == bgp_show_type_regexp
  5575. || type == bgp_show_type_flap_regexp)
  5576. {
  5577. regex_t *regex = output_arg;
  5578. if (bgp_regexec (regex, ri->attr->aspath) == REG_NOMATCH)
  5579. continue;
  5580. }
  5581. if (type == bgp_show_type_prefix_list
  5582. || type == bgp_show_type_flap_prefix_list)
  5583. {
  5584. struct prefix_list *plist = output_arg;
  5585. if (prefix_list_apply (plist, &rn->p) != PREFIX_PERMIT)
  5586. continue;
  5587. }
  5588. if (type == bgp_show_type_filter_list
  5589. || type == bgp_show_type_flap_filter_list)
  5590. {
  5591. struct as_list *as_list = output_arg;
  5592. if (as_list_apply (as_list, ri->attr->aspath) != AS_FILTER_PERMIT)
  5593. continue;
  5594. }
  5595. if (type == bgp_show_type_route_map
  5596. || type == bgp_show_type_flap_route_map)
  5597. {
  5598. struct route_map *rmap = output_arg;
  5599. struct bgp_info binfo;
  5600. struct attr dummy_attr;
  5601. struct attr_extra dummy_extra;
  5602. int ret;
  5603. dummy_attr.extra = &dummy_extra;
  5604. bgp_attr_dup (&dummy_attr, ri->attr);
  5605. binfo.peer = ri->peer;
  5606. binfo.attr = &dummy_attr;
  5607. ret = route_map_apply (rmap, &rn->p, RMAP_BGP, &binfo);
  5608. if (ret == RMAP_DENYMATCH)
  5609. continue;
  5610. }
  5611. if (type == bgp_show_type_neighbor
  5612. || type == bgp_show_type_flap_neighbor
  5613. || type == bgp_show_type_damp_neighbor)
  5614. {
  5615. union sockunion *su = output_arg;
  5616. if (ri->peer->su_remote == NULL || ! sockunion_same(ri->peer->su_remote, su))
  5617. continue;
  5618. }
  5619. if (type == bgp_show_type_cidr_only
  5620. || type == bgp_show_type_flap_cidr_only)
  5621. {
  5622. u_int32_t destination;
  5623. destination = ntohl (rn->p.u.prefix4.s_addr);
  5624. if (IN_CLASSC (destination) && rn->p.prefixlen == 24)
  5625. continue;
  5626. if (IN_CLASSB (destination) && rn->p.prefixlen == 16)
  5627. continue;
  5628. if (IN_CLASSA (destination) && rn->p.prefixlen == 8)
  5629. continue;
  5630. }
  5631. if (type == bgp_show_type_prefix_longer
  5632. || type == bgp_show_type_flap_prefix_longer)
  5633. {
  5634. struct prefix *p = output_arg;
  5635. if (! prefix_match (p, &rn->p))
  5636. continue;
  5637. }
  5638. if (type == bgp_show_type_community_all)
  5639. {
  5640. if (! ri->attr->community)
  5641. continue;
  5642. }
  5643. if (type == bgp_show_type_community)
  5644. {
  5645. struct community *com = output_arg;
  5646. if (! ri->attr->community ||
  5647. ! community_match (ri->attr->community, com))
  5648. continue;
  5649. }
  5650. if (type == bgp_show_type_community_exact)
  5651. {
  5652. struct community *com = output_arg;
  5653. if (! ri->attr->community ||
  5654. ! community_cmp (ri->attr->community, com))
  5655. continue;
  5656. }
  5657. if (type == bgp_show_type_community_list)
  5658. {
  5659. struct community_list *list = output_arg;
  5660. if (! community_list_match (ri->attr->community, list))
  5661. continue;
  5662. }
  5663. if (type == bgp_show_type_community_list_exact)
  5664. {
  5665. struct community_list *list = output_arg;
  5666. if (! community_list_exact_match (ri->attr->community, list))
  5667. continue;
  5668. }
  5669. if (type == bgp_show_type_flap_address
  5670. || type == bgp_show_type_flap_prefix)
  5671. {
  5672. struct prefix *p = output_arg;
  5673. if (! prefix_match (&rn->p, p))
  5674. continue;
  5675. if (type == bgp_show_type_flap_prefix)
  5676. if (p->prefixlen != rn->p.prefixlen)
  5677. continue;
  5678. }
  5679. if (type == bgp_show_type_dampend_paths
  5680. || type == bgp_show_type_damp_neighbor)
  5681. {
  5682. if (! CHECK_FLAG (ri->flags, BGP_INFO_DAMPED)
  5683. || CHECK_FLAG (ri->flags, BGP_INFO_HISTORY))
  5684. continue;
  5685. }
  5686. if (header)
  5687. {
  5688. vty_out (vty, "BGP table version is 0, local router ID is %s%s", inet_ntoa (*router_id), VTY_NEWLINE);
  5689. vty_out (vty, BGP_SHOW_SCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
  5690. vty_out (vty, BGP_SHOW_OCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE);
  5691. if (type == bgp_show_type_dampend_paths
  5692. || type == bgp_show_type_damp_neighbor)
  5693. vty_out (vty, BGP_SHOW_DAMP_HEADER, VTY_NEWLINE);
  5694. else if (type == bgp_show_type_flap_statistics
  5695. || type == bgp_show_type_flap_address
  5696. || type == bgp_show_type_flap_prefix
  5697. || type == bgp_show_type_flap_cidr_only
  5698. || type == bgp_show_type_flap_regexp
  5699. || type == bgp_show_type_flap_filter_list
  5700. || type == bgp_show_type_flap_prefix_list
  5701. || type == bgp_show_type_flap_prefix_longer
  5702. || type == bgp_show_type_flap_route_map
  5703. || type == bgp_show_type_flap_neighbor)
  5704. vty_out (vty, BGP_SHOW_FLAP_HEADER, VTY_NEWLINE);
  5705. else
  5706. vty_out (vty, BGP_SHOW_HEADER, VTY_NEWLINE);
  5707. header = 0;
  5708. }
  5709. if (type == bgp_show_type_dampend_paths
  5710. || type == bgp_show_type_damp_neighbor)
  5711. damp_route_vty_out (vty, &rn->p, ri, display, SAFI_UNICAST);
  5712. else if (type == bgp_show_type_flap_statistics
  5713. || type == bgp_show_type_flap_address
  5714. || type == bgp_show_type_flap_prefix
  5715. || type == bgp_show_type_flap_cidr_only
  5716. || type == bgp_show_type_flap_regexp
  5717. || type == bgp_show_type_flap_filter_list
  5718. || type == bgp_show_type_flap_prefix_list
  5719. || type == bgp_show_type_flap_prefix_longer
  5720. || type == bgp_show_type_flap_route_map
  5721. || type == bgp_show_type_flap_neighbor)
  5722. flap_route_vty_out (vty, &rn->p, ri, display, SAFI_UNICAST);
  5723. else
  5724. route_vty_out (vty, &rn->p, ri, display, SAFI_UNICAST);
  5725. display++;
  5726. }
  5727. if (display)
  5728. output_count++;
  5729. }
  5730. /* No route is displayed */
  5731. if (output_count == 0)
  5732. {
  5733. if (type == bgp_show_type_normal)
  5734. vty_out (vty, "No BGP prefixes displayed, %ld exist%s", total_count, VTY_NEWLINE);
  5735. }
  5736. else
  5737. vty_out (vty, "%sDisplayed %ld out of %ld total prefixes%s",
  5738. VTY_NEWLINE, output_count, total_count, VTY_NEWLINE);
  5739. return CMD_SUCCESS;
  5740. }
  5741. static int
  5742. bgp_show (struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,
  5743. enum bgp_show_type type, void *output_arg)
  5744. {
  5745. struct bgp_table *table;
  5746. if (bgp == NULL) {
  5747. bgp = bgp_get_default ();
  5748. }
  5749. if (bgp == NULL)
  5750. {
  5751. vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
  5752. return CMD_WARNING;
  5753. }
  5754. table = bgp->rib[afi][safi];
  5755. return bgp_show_table (vty, table, &bgp->router_id, type, output_arg);
  5756. }
  5757. /* Header of detailed BGP route information */
  5758. static void
  5759. route_vty_out_detail_header (struct vty *vty, struct bgp *bgp,
  5760. struct bgp_node *rn,
  5761. struct prefix_rd *prd, afi_t afi, safi_t safi)
  5762. {
  5763. struct bgp_info *ri;
  5764. struct prefix *p;
  5765. struct peer *peer;
  5766. struct listnode *node, *nnode;
  5767. char buf1[INET6_ADDRSTRLEN];
  5768. char buf2[INET6_ADDRSTRLEN];
  5769. int count = 0;
  5770. int best = 0;
  5771. int suppress = 0;
  5772. int no_export = 0;
  5773. int no_advertise = 0;
  5774. int local_as = 0;
  5775. int first = 0;
  5776. int printrd = ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP));
  5777. p = &rn->p;
  5778. vty_out (vty, "BGP routing table entry for %s%s%s/%d%s",
  5779. (printrd ? prefix_rd2str (prd, buf1, RD_ADDRSTRLEN) : ""),
  5780. printrd ? ":" : "",
  5781. inet_ntop (p->family, &p->u.prefix, buf2, INET6_ADDRSTRLEN),
  5782. p->prefixlen, VTY_NEWLINE);
  5783. for (ri = rn->info; ri; ri = ri->next)
  5784. {
  5785. count++;
  5786. if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))
  5787. {
  5788. best = count;
  5789. if (ri->extra && ri->extra->suppress)
  5790. suppress = 1;
  5791. if (ri->attr->community != NULL)
  5792. {
  5793. if (community_include (ri->attr->community, COMMUNITY_NO_ADVERTISE))
  5794. no_advertise = 1;
  5795. if (community_include (ri->attr->community, COMMUNITY_NO_EXPORT))
  5796. no_export = 1;
  5797. if (community_include (ri->attr->community, COMMUNITY_LOCAL_AS))
  5798. local_as = 1;
  5799. }
  5800. }
  5801. }
  5802. vty_out (vty, "Paths: (%d available", count);
  5803. if (best)
  5804. {
  5805. vty_out (vty, ", best #%d", best);
  5806. if (safi == SAFI_UNICAST)
  5807. vty_out (vty, ", table Default-IP-Routing-Table");
  5808. }
  5809. else
  5810. vty_out (vty, ", no best path");
  5811. if (no_advertise)
  5812. vty_out (vty, ", not advertised to any peer");
  5813. else if (no_export)
  5814. vty_out (vty, ", not advertised to EBGP peer");
  5815. else if (local_as)
  5816. vty_out (vty, ", not advertised outside local AS");
  5817. if (suppress)
  5818. vty_out (vty, ", Advertisements suppressed by an aggregate.");
  5819. vty_out (vty, ")%s", VTY_NEWLINE);
  5820. /* advertised peer */
  5821. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  5822. {
  5823. if (bgp_adj_out_lookup (peer, p, afi, safi, rn))
  5824. {
  5825. if (! first)
  5826. vty_out (vty, " Advertised to non peer-group peers:%s ", VTY_NEWLINE);
  5827. vty_out (vty, " %s", sockunion2str (&peer->su, buf1, SU_ADDRSTRLEN));
  5828. first = 1;
  5829. }
  5830. }
  5831. if (! first)
  5832. vty_out (vty, " Not advertised to any peer");
  5833. vty_out (vty, "%s", VTY_NEWLINE);
  5834. }
  5835. /* Display specified route of BGP table. */
  5836. static int
  5837. bgp_show_route_in_table (struct vty *vty, struct bgp *bgp,
  5838. struct bgp_table *rib, const char *ip_str,
  5839. afi_t afi, safi_t safi, struct prefix_rd *prd,
  5840. int prefix_check, enum bgp_path_type pathtype)
  5841. {
  5842. int ret;
  5843. int header;
  5844. int display = 0;
  5845. struct prefix match;
  5846. struct bgp_node *rn;
  5847. struct bgp_node *rm;
  5848. struct bgp_info *ri;
  5849. struct bgp_table *table;
  5850. memset (&match, 0, sizeof (struct prefix)); /* keep valgrind happy */
  5851. /* Check IP address argument. */
  5852. ret = str2prefix (ip_str, &match);
  5853. if (! ret)
  5854. {
  5855. vty_out (vty, "address is malformed%s", VTY_NEWLINE);
  5856. return CMD_WARNING;
  5857. }
  5858. match.family = afi2family (afi);
  5859. if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
  5860. {
  5861. for (rn = bgp_table_top (rib); rn; rn = bgp_route_next (rn))
  5862. {
  5863. if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
  5864. continue;
  5865. if ((table = rn->info) != NULL)
  5866. {
  5867. header = 1;
  5868. if ((rm = bgp_node_match (table, &match)) != NULL)
  5869. {
  5870. if (prefix_check && rm->p.prefixlen != match.prefixlen)
  5871. {
  5872. bgp_unlock_node (rm);
  5873. continue;
  5874. }
  5875. for (ri = rm->info; ri; ri = ri->next)
  5876. {
  5877. if (header)
  5878. {
  5879. route_vty_out_detail_header (vty, bgp, rm, (struct prefix_rd *)&rn->p,
  5880. AFI_IP, safi);
  5881. header = 0;
  5882. }
  5883. display++;
  5884. if (pathtype == BGP_PATH_ALL ||
  5885. (pathtype == BGP_PATH_BESTPATH && CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)) ||
  5886. (pathtype == BGP_PATH_MULTIPATH &&
  5887. (CHECK_FLAG (ri->flags, BGP_INFO_MULTIPATH) || CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))))
  5888. route_vty_out_detail (vty, bgp, &rm->p, ri, AFI_IP, safi);
  5889. }
  5890. bgp_unlock_node (rm);
  5891. }
  5892. }
  5893. }
  5894. }
  5895. else
  5896. {
  5897. header = 1;
  5898. if ((rn = bgp_node_match (rib, &match)) != NULL)
  5899. {
  5900. if (! prefix_check || rn->p.prefixlen == match.prefixlen)
  5901. {
  5902. for (ri = rn->info; ri; ri = ri->next)
  5903. {
  5904. if (header)
  5905. {
  5906. route_vty_out_detail_header (vty, bgp, rn, NULL, afi, safi);
  5907. header = 0;
  5908. }
  5909. display++;
  5910. if (pathtype == BGP_PATH_ALL ||
  5911. (pathtype == BGP_PATH_BESTPATH && CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)) ||
  5912. (pathtype == BGP_PATH_MULTIPATH &&
  5913. (CHECK_FLAG (ri->flags, BGP_INFO_MULTIPATH) || CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))))
  5914. route_vty_out_detail (vty, bgp, &rn->p, ri, afi, safi);
  5915. }
  5916. }
  5917. bgp_unlock_node (rn);
  5918. }
  5919. }
  5920. if (! display)
  5921. {
  5922. vty_out (vty, "%% Network not in table%s", VTY_NEWLINE);
  5923. return CMD_WARNING;
  5924. }
  5925. return CMD_SUCCESS;
  5926. }
  5927. /* Display specified route of Main RIB */
  5928. static int
  5929. bgp_show_route (struct vty *vty, const char *view_name, const char *ip_str,
  5930. afi_t afi, safi_t safi, struct prefix_rd *prd,
  5931. int prefix_check, enum bgp_path_type pathtype)
  5932. {
  5933. struct bgp *bgp;
  5934. /* BGP structure lookup. */
  5935. if (view_name)
  5936. {
  5937. bgp = bgp_lookup_by_name (view_name);
  5938. if (bgp == NULL)
  5939. {
  5940. vty_out (vty, "Can't find BGP view %s%s", view_name, VTY_NEWLINE);
  5941. return CMD_WARNING;
  5942. }
  5943. }
  5944. else
  5945. {
  5946. bgp = bgp_get_default ();
  5947. if (bgp == NULL)
  5948. {
  5949. vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
  5950. return CMD_WARNING;
  5951. }
  5952. }
  5953. return bgp_show_route_in_table (vty, bgp, bgp->rib[afi][safi], ip_str,
  5954. afi, safi, prd, prefix_check, pathtype);
  5955. }
  5956. /* BGP route print out function. */
  5957. DEFUN (show_ip_bgp,
  5958. show_ip_bgp_cmd,
  5959. "show ip bgp",
  5960. SHOW_STR
  5961. IP_STR
  5962. BGP_STR)
  5963. {
  5964. return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
  5965. }
  5966. DEFUN (show_ip_bgp_ipv4,
  5967. show_ip_bgp_ipv4_cmd,
  5968. "show ip bgp ipv4 (unicast|multicast)",
  5969. SHOW_STR
  5970. IP_STR
  5971. BGP_STR
  5972. "Address family\n"
  5973. "Address Family modifier\n"
  5974. "Address Family modifier\n")
  5975. {
  5976. if (strncmp (argv[0], "m", 1) == 0)
  5977. return bgp_show (vty, NULL, AFI_IP, SAFI_MULTICAST, bgp_show_type_normal,
  5978. NULL);
  5979. return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
  5980. }
  5981. DEFUN (show_ip_bgp_route,
  5982. show_ip_bgp_route_cmd,
  5983. "show ip bgp A.B.C.D",
  5984. SHOW_STR
  5985. IP_STR
  5986. BGP_STR
  5987. "Network in the BGP routing table to display\n")
  5988. {
  5989. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  5990. }
  5991. DEFUN (show_ip_bgp_route_pathtype,
  5992. show_ip_bgp_route_pathtype_cmd,
  5993. "show ip bgp A.B.C.D (bestpath|multipath)",
  5994. SHOW_STR
  5995. IP_STR
  5996. BGP_STR
  5997. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  5998. "Display only the bestpath\n"
  5999. "Display only multipaths\n")
  6000. {
  6001. if (strncmp (argv[1], "b", 1) == 0)
  6002. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_BESTPATH);
  6003. else
  6004. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_MULTIPATH);
  6005. }
  6006. DEFUN (show_bgp_ipv4_safi_route_pathtype,
  6007. show_bgp_ipv4_safi_route_pathtype_cmd,
  6008. "show bgp ipv4 (unicast|multicast) A.B.C.D (bestpath|multipath)",
  6009. SHOW_STR
  6010. BGP_STR
  6011. "Address family\n"
  6012. "Address Family modifier\n"
  6013. "Address Family modifier\n"
  6014. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  6015. "Display only the bestpath\n"
  6016. "Display only multipaths\n")
  6017. {
  6018. if (strncmp (argv[0], "m", 1) == 0)
  6019. if (strncmp (argv[2], "b", 1) == 0)
  6020. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 0, BGP_PATH_BESTPATH);
  6021. else
  6022. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 0, BGP_PATH_MULTIPATH);
  6023. else
  6024. if (strncmp (argv[2], "b", 1) == 0)
  6025. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_BESTPATH);
  6026. else
  6027. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_MULTIPATH);
  6028. }
  6029. DEFUN (show_ip_bgp_ipv4_route,
  6030. show_ip_bgp_ipv4_route_cmd,
  6031. "show ip bgp ipv4 (unicast|multicast) A.B.C.D",
  6032. SHOW_STR
  6033. IP_STR
  6034. BGP_STR
  6035. "Address family\n"
  6036. "Address Family modifier\n"
  6037. "Address Family modifier\n"
  6038. "Network in the BGP routing table to display\n")
  6039. {
  6040. if (strncmp (argv[0], "m", 1) == 0)
  6041. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 0, BGP_PATH_ALL);
  6042. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6043. }
  6044. DEFUN (show_ip_bgp_vpnv4_all_route,
  6045. show_ip_bgp_vpnv4_all_route_cmd,
  6046. "show ip bgp vpnv4 all A.B.C.D",
  6047. SHOW_STR
  6048. IP_STR
  6049. BGP_STR
  6050. "Display VPNv4 NLRI specific information\n"
  6051. "Display information about all VPNv4 NLRIs\n"
  6052. "Network in the BGP routing table to display\n")
  6053. {
  6054. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_MPLS_VPN, NULL, 0, BGP_PATH_ALL);
  6055. }
  6056. DEFUN (show_ip_bgp_vpnv4_rd_route,
  6057. show_ip_bgp_vpnv4_rd_route_cmd,
  6058. "show ip bgp vpnv4 rd ASN:nn_or_IP-address:nn A.B.C.D",
  6059. SHOW_STR
  6060. IP_STR
  6061. BGP_STR
  6062. "Display VPNv4 NLRI specific information\n"
  6063. "Display information for a route distinguisher\n"
  6064. "VPN Route Distinguisher\n"
  6065. "Network in the BGP routing table to display\n")
  6066. {
  6067. int ret;
  6068. struct prefix_rd prd;
  6069. ret = str2prefix_rd (argv[0], &prd);
  6070. if (! ret)
  6071. {
  6072. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6073. return CMD_WARNING;
  6074. }
  6075. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MPLS_VPN, &prd, 0, BGP_PATH_ALL);
  6076. }
  6077. DEFUN (show_ip_bgp_prefix,
  6078. show_ip_bgp_prefix_cmd,
  6079. "show ip bgp A.B.C.D/M",
  6080. SHOW_STR
  6081. IP_STR
  6082. BGP_STR
  6083. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6084. {
  6085. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6086. }
  6087. DEFUN (show_ip_bgp_prefix_pathtype,
  6088. show_ip_bgp_prefix_pathtype_cmd,
  6089. "show ip bgp A.B.C.D/M (bestpath|multipath)",
  6090. SHOW_STR
  6091. IP_STR
  6092. BGP_STR
  6093. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  6094. "Display only the bestpath\n"
  6095. "Display only multipaths\n")
  6096. {
  6097. if (strncmp (argv[1], "b", 1) == 0)
  6098. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_BESTPATH);
  6099. else
  6100. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_MULTIPATH);
  6101. }
  6102. DEFUN (show_ip_bgp_ipv4_prefix,
  6103. show_ip_bgp_ipv4_prefix_cmd,
  6104. "show ip bgp ipv4 (unicast|multicast) A.B.C.D/M",
  6105. SHOW_STR
  6106. IP_STR
  6107. BGP_STR
  6108. "Address family\n"
  6109. "Address Family modifier\n"
  6110. "Address Family modifier\n"
  6111. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6112. {
  6113. if (strncmp (argv[0], "m", 1) == 0)
  6114. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 1, BGP_PATH_ALL);
  6115. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6116. }
  6117. DEFUN (show_ip_bgp_ipv4_prefix_pathtype,
  6118. show_ip_bgp_ipv4_prefix_pathtype_cmd,
  6119. "show ip bgp ipv4 (unicast|multicast) A.B.C.D/M (bestpath|multipath)",
  6120. SHOW_STR
  6121. IP_STR
  6122. BGP_STR
  6123. "Address family\n"
  6124. "Address Family modifier\n"
  6125. "Address Family modifier\n"
  6126. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  6127. "Display only the bestpath\n"
  6128. "Display only multipaths\n")
  6129. {
  6130. if (strncmp (argv[0], "m", 1) == 0)
  6131. if (strncmp (argv[2], "b", 1) == 0)
  6132. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 1, BGP_PATH_BESTPATH);
  6133. else
  6134. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 1, BGP_PATH_MULTIPATH);
  6135. else
  6136. if (strncmp (argv[2], "b", 1) == 0)
  6137. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_BESTPATH);
  6138. else
  6139. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_MULTIPATH);
  6140. }
  6141. ALIAS (show_ip_bgp_ipv4_prefix_pathtype,
  6142. show_bgp_ipv4_safi_prefix_pathtype_cmd,
  6143. "show bgp ipv4 (unicast|multicast) A.B.C.D/M (bestpath|multipath)",
  6144. SHOW_STR
  6145. BGP_STR
  6146. "Address family\n"
  6147. "Address Family modifier\n"
  6148. "Address Family modifier\n"
  6149. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
  6150. "Display only the bestpath\n"
  6151. "Display only multipaths\n")
  6152. DEFUN (show_ip_bgp_vpnv4_all_prefix,
  6153. show_ip_bgp_vpnv4_all_prefix_cmd,
  6154. "show ip bgp vpnv4 all A.B.C.D/M",
  6155. SHOW_STR
  6156. IP_STR
  6157. BGP_STR
  6158. "Display VPNv4 NLRI specific information\n"
  6159. "Display information about all VPNv4 NLRIs\n"
  6160. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6161. {
  6162. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_MPLS_VPN, NULL, 1, BGP_PATH_ALL);
  6163. }
  6164. DEFUN (show_ip_bgp_vpnv4_rd_prefix,
  6165. show_ip_bgp_vpnv4_rd_prefix_cmd,
  6166. "show ip bgp vpnv4 rd ASN:nn_or_IP-address:nn A.B.C.D/M",
  6167. SHOW_STR
  6168. IP_STR
  6169. BGP_STR
  6170. "Display VPNv4 NLRI specific information\n"
  6171. "Display information for a route distinguisher\n"
  6172. "VPN Route Distinguisher\n"
  6173. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6174. {
  6175. int ret;
  6176. struct prefix_rd prd;
  6177. ret = str2prefix_rd (argv[0], &prd);
  6178. if (! ret)
  6179. {
  6180. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6181. return CMD_WARNING;
  6182. }
  6183. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MPLS_VPN, &prd, 1, BGP_PATH_ALL);
  6184. }
  6185. DEFUN (show_ip_bgp_view,
  6186. show_ip_bgp_view_cmd,
  6187. "show ip bgp view WORD",
  6188. SHOW_STR
  6189. IP_STR
  6190. BGP_STR
  6191. "BGP view\n"
  6192. "View name\n")
  6193. {
  6194. struct bgp *bgp;
  6195. /* BGP structure lookup. */
  6196. bgp = bgp_lookup_by_name (argv[0]);
  6197. if (bgp == NULL)
  6198. {
  6199. vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
  6200. return CMD_WARNING;
  6201. }
  6202. return bgp_show (vty, bgp, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
  6203. }
  6204. DEFUN (show_ip_bgp_view_route,
  6205. show_ip_bgp_view_route_cmd,
  6206. "show ip bgp view WORD A.B.C.D",
  6207. SHOW_STR
  6208. IP_STR
  6209. BGP_STR
  6210. "BGP view\n"
  6211. "View name\n"
  6212. "Network in the BGP routing table to display\n")
  6213. {
  6214. return bgp_show_route (vty, argv[0], argv[1], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6215. }
  6216. DEFUN (show_ip_bgp_view_prefix,
  6217. show_ip_bgp_view_prefix_cmd,
  6218. "show ip bgp view WORD A.B.C.D/M",
  6219. SHOW_STR
  6220. IP_STR
  6221. BGP_STR
  6222. "BGP view\n"
  6223. "View name\n"
  6224. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6225. {
  6226. return bgp_show_route (vty, argv[0], argv[1], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6227. }
  6228. DEFUN (show_bgp,
  6229. show_bgp_cmd,
  6230. "show bgp",
  6231. SHOW_STR
  6232. BGP_STR)
  6233. {
  6234. return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal,
  6235. NULL);
  6236. }
  6237. ALIAS (show_bgp,
  6238. show_bgp_ipv6_cmd,
  6239. "show bgp ipv6",
  6240. SHOW_STR
  6241. BGP_STR
  6242. "Address family\n")
  6243. /* old command */
  6244. DEFUN (show_ipv6_bgp,
  6245. show_ipv6_bgp_cmd,
  6246. "show ipv6 bgp",
  6247. SHOW_STR
  6248. IP_STR
  6249. BGP_STR)
  6250. {
  6251. return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal,
  6252. NULL);
  6253. }
  6254. DEFUN (show_bgp_route,
  6255. show_bgp_route_cmd,
  6256. "show bgp X:X::X:X",
  6257. SHOW_STR
  6258. BGP_STR
  6259. "Network in the BGP routing table to display\n")
  6260. {
  6261. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6262. }
  6263. DEFUN (show_bgp_ipv4_safi,
  6264. show_bgp_ipv4_safi_cmd,
  6265. "show bgp ipv4 (unicast|multicast)",
  6266. SHOW_STR
  6267. BGP_STR
  6268. "Address family\n"
  6269. "Address Family modifier\n"
  6270. "Address Family modifier\n")
  6271. {
  6272. if (strncmp (argv[0], "m", 1) == 0)
  6273. return bgp_show (vty, NULL, AFI_IP, SAFI_MULTICAST, bgp_show_type_normal,
  6274. NULL);
  6275. return bgp_show (vty, NULL, AFI_IP, SAFI_UNICAST, bgp_show_type_normal, NULL);
  6276. }
  6277. DEFUN (show_bgp_ipv4_safi_route,
  6278. show_bgp_ipv4_safi_route_cmd,
  6279. "show bgp ipv4 (unicast|multicast) A.B.C.D",
  6280. SHOW_STR
  6281. BGP_STR
  6282. "Address family\n"
  6283. "Address Family modifier\n"
  6284. "Address Family modifier\n"
  6285. "Network in the BGP routing table to display\n")
  6286. {
  6287. if (strncmp (argv[0], "m", 1) == 0)
  6288. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 0, BGP_PATH_ALL);
  6289. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6290. }
  6291. DEFUN (show_bgp_route_pathtype,
  6292. show_bgp_route_pathtype_cmd,
  6293. "show bgp X:X::X:X (bestpath|multipath)",
  6294. SHOW_STR
  6295. BGP_STR
  6296. "Network in the BGP routing table to display\n"
  6297. "Display only the bestpath\n"
  6298. "Display only multipaths\n")
  6299. {
  6300. if (strncmp (argv[1], "b", 1) == 0)
  6301. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_BESTPATH);
  6302. else
  6303. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_MULTIPATH);
  6304. }
  6305. ALIAS (show_bgp_route_pathtype,
  6306. show_bgp_ipv6_route_pathtype_cmd,
  6307. "show bgp ipv6 X:X::X:X (bestpath|multipath)",
  6308. SHOW_STR
  6309. BGP_STR
  6310. "Address family\n"
  6311. "Network in the BGP routing table to display\n"
  6312. "Display only the bestpath\n"
  6313. "Display only multipaths\n")
  6314. DEFUN (show_bgp_ipv6_safi_route_pathtype,
  6315. show_bgp_ipv6_safi_route_pathtype_cmd,
  6316. "show bgp ipv6 (unicast|multicast) X:X::X:X (bestpath|multipath)",
  6317. SHOW_STR
  6318. BGP_STR
  6319. "Address family\n"
  6320. "Address Family modifier\n"
  6321. "Address Family modifier\n"
  6322. "Network in the BGP routing table to display\n"
  6323. "Display only the bestpath\n"
  6324. "Display only multipaths\n")
  6325. {
  6326. if (strncmp (argv[0], "m", 1) == 0)
  6327. if (strncmp (argv[2], "b", 1) == 0)
  6328. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 0, BGP_PATH_BESTPATH);
  6329. else
  6330. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 0, BGP_PATH_MULTIPATH);
  6331. else
  6332. if (strncmp (argv[2], "b", 1) == 0)
  6333. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_BESTPATH);
  6334. else
  6335. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_MULTIPATH);
  6336. }
  6337. DEFUN (show_bgp_ipv4_vpn_route,
  6338. show_bgp_ipv4_vpn_route_cmd,
  6339. "show bgp ipv4 vpn A.B.C.D",
  6340. SHOW_STR
  6341. BGP_STR
  6342. "Address Family\n"
  6343. "Display VPN NLRI specific information\n"
  6344. "Network in the BGP routing table to display\n")
  6345. {
  6346. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_MPLS_VPN, NULL, 0, BGP_PATH_ALL);
  6347. }
  6348. DEFUN (show_bgp_ipv6_vpn_route,
  6349. show_bgp_ipv6_vpn_route_cmd,
  6350. "show bgp ipv6 vpn X:X::X:X",
  6351. SHOW_STR
  6352. BGP_STR
  6353. "Address Family\n"
  6354. "Display VPN NLRI specific information\n"
  6355. "Network in the BGP routing table to display\n")
  6356. {
  6357. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_MPLS_VPN, NULL, 0, BGP_PATH_ALL);
  6358. }
  6359. DEFUN (show_bgp_ipv4_vpn_rd_route,
  6360. show_bgp_ipv4_vpn_rd_route_cmd,
  6361. "show bgp ipv4 vpn rd ASN:nn_or_IP-address:nn A.B.C.D",
  6362. SHOW_STR
  6363. BGP_STR
  6364. IP_STR
  6365. "Display VPN NLRI specific information\n"
  6366. "Display information for a route distinguisher\n"
  6367. "VPN Route Distinguisher\n"
  6368. "Network in the BGP routing table to display\n")
  6369. {
  6370. int ret;
  6371. struct prefix_rd prd;
  6372. ret = str2prefix_rd (argv[0], &prd);
  6373. if (! ret)
  6374. {
  6375. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6376. return CMD_WARNING;
  6377. }
  6378. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MPLS_VPN, &prd, 0, BGP_PATH_ALL);
  6379. }
  6380. DEFUN (show_bgp_ipv6_vpn_rd_route,
  6381. show_bgp_ipv6_vpn_rd_route_cmd,
  6382. "show bgp ipv6 vpn rd ASN:nn_or_IP-address:nn X:X::X:X",
  6383. SHOW_STR
  6384. BGP_STR
  6385. "Address Family\n"
  6386. "Display VPN NLRI specific information\n"
  6387. "Display information for a route distinguisher\n"
  6388. "VPN Route Distinguisher\n"
  6389. "Network in the BGP routing table to display\n")
  6390. {
  6391. int ret;
  6392. struct prefix_rd prd;
  6393. ret = str2prefix_rd (argv[0], &prd);
  6394. if (! ret)
  6395. {
  6396. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6397. return CMD_WARNING;
  6398. }
  6399. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MPLS_VPN, &prd, 0, BGP_PATH_ALL);
  6400. }
  6401. DEFUN (show_bgp_prefix_pathtype,
  6402. show_bgp_prefix_pathtype_cmd,
  6403. "show bgp X:X::X:X/M (bestpath|multipath)",
  6404. SHOW_STR
  6405. BGP_STR
  6406. "IPv6 prefix <network>/<length>\n"
  6407. "Display only the bestpath\n"
  6408. "Display only multipaths\n")
  6409. {
  6410. if (strncmp (argv[1], "b", 1) == 0)
  6411. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_BESTPATH);
  6412. else
  6413. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_MULTIPATH);
  6414. }
  6415. ALIAS (show_bgp_prefix_pathtype,
  6416. show_bgp_ipv6_prefix_pathtype_cmd,
  6417. "show bgp ipv6 X:X::X:X/M (bestpath|multipath)",
  6418. SHOW_STR
  6419. BGP_STR
  6420. "Address family\n"
  6421. "IPv6 prefix <network>/<length>\n"
  6422. "Display only the bestpath\n"
  6423. "Display only multipaths\n")
  6424. DEFUN (show_bgp_ipv6_safi_prefix_pathtype,
  6425. show_bgp_ipv6_safi_prefix_pathtype_cmd,
  6426. "show bgp ipv6 (unicast|multicast) X:X::X:X/M (bestpath|multipath)",
  6427. SHOW_STR
  6428. BGP_STR
  6429. "Address family\n"
  6430. "Address Family modifier\n"
  6431. "Address Family modifier\n"
  6432. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
  6433. "Display only the bestpath\n"
  6434. "Display only multipaths\n")
  6435. {
  6436. if (strncmp (argv[0], "m", 1) == 0)
  6437. if (strncmp (argv[2], "b", 1) == 0)
  6438. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 1, BGP_PATH_BESTPATH);
  6439. else
  6440. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 1, BGP_PATH_MULTIPATH);
  6441. else
  6442. if (strncmp (argv[2], "b", 1) == 0)
  6443. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_BESTPATH);
  6444. else
  6445. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_MULTIPATH);
  6446. }
  6447. DEFUN (show_bgp_ipv4_encap_route,
  6448. show_bgp_ipv4_encap_route_cmd,
  6449. "show bgp ipv4 encap A.B.C.D",
  6450. SHOW_STR
  6451. BGP_STR
  6452. IP_STR
  6453. "Display ENCAP NLRI specific information\n"
  6454. "Network in the BGP routing table to display\n")
  6455. {
  6456. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_ENCAP, NULL, 0, BGP_PATH_ALL);
  6457. }
  6458. DEFUN (show_bgp_ipv6_encap_route,
  6459. show_bgp_ipv6_encap_route_cmd,
  6460. "show bgp ipv6 encap X:X::X:X",
  6461. SHOW_STR
  6462. BGP_STR
  6463. IP6_STR
  6464. "Display ENCAP NLRI specific information\n"
  6465. "Network in the BGP routing table to display\n")
  6466. {
  6467. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_ENCAP, NULL, 0, BGP_PATH_ALL);
  6468. }
  6469. DEFUN (show_bgp_ipv4_safi_rd_route,
  6470. show_bgp_ipv4_safi_rd_route_cmd,
  6471. "show bgp ipv4 (encap|vpn) rd ASN:nn_or_IP-address:nn A.B.C.D",
  6472. SHOW_STR
  6473. BGP_STR
  6474. "Address Family\n"
  6475. "Address Family Modifier\n"
  6476. "Address Family Modifier\n"
  6477. "Display information for a route distinguisher\n"
  6478. "ENCAP Route Distinguisher\n"
  6479. "Network in the BGP routing table to display\n")
  6480. {
  6481. int ret;
  6482. struct prefix_rd prd;
  6483. safi_t safi;
  6484. if (bgp_parse_safi(argv[0], &safi)) {
  6485. vty_out (vty, "Error: Bad SAFI: %s%s", argv[0], VTY_NEWLINE);
  6486. return CMD_WARNING;
  6487. }
  6488. ret = str2prefix_rd (argv[1], &prd);
  6489. if (! ret)
  6490. {
  6491. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6492. return CMD_WARNING;
  6493. }
  6494. return bgp_show_route (vty, NULL, argv[2], AFI_IP, safi, &prd, 0, BGP_PATH_ALL);
  6495. }
  6496. DEFUN (show_bgp_ipv6_safi_rd_route,
  6497. show_bgp_ipv6_safi_rd_route_cmd,
  6498. "show bgp ipv6 (encap|vpn) rd ASN:nn_or_IP-address:nn X:X::X:X",
  6499. SHOW_STR
  6500. BGP_STR
  6501. "Address Family\n"
  6502. "Address Family Modifier\n"
  6503. "Address Family Modifier\n"
  6504. "Display information for a route distinguisher\n"
  6505. "ENCAP Route Distinguisher\n"
  6506. "Network in the BGP routing table to display\n")
  6507. {
  6508. int ret;
  6509. struct prefix_rd prd;
  6510. safi_t safi;
  6511. if (bgp_parse_safi(argv[0], &safi)) {
  6512. vty_out (vty, "Error: Bad SAFI: %s%s", argv[0], VTY_NEWLINE);
  6513. return CMD_WARNING;
  6514. }
  6515. ret = str2prefix_rd (argv[1], &prd);
  6516. if (! ret)
  6517. {
  6518. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6519. return CMD_WARNING;
  6520. }
  6521. return bgp_show_route (vty, NULL, argv[2], AFI_IP6, SAFI_ENCAP, &prd, 0, BGP_PATH_ALL);
  6522. }
  6523. DEFUN (show_bgp_ipv4_prefix,
  6524. show_bgp_ipv4_prefix_cmd,
  6525. "show bgp ipv4 A.B.C.D/M",
  6526. SHOW_STR
  6527. BGP_STR
  6528. IP_STR
  6529. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6530. {
  6531. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6532. }
  6533. DEFUN (show_bgp_ipv4_safi_prefix,
  6534. show_bgp_ipv4_safi_prefix_cmd,
  6535. "show bgp ipv4 (unicast|multicast) A.B.C.D/M",
  6536. SHOW_STR
  6537. BGP_STR
  6538. "Address family\n"
  6539. "Address Family modifier\n"
  6540. "Address Family modifier\n"
  6541. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6542. {
  6543. if (strncmp (argv[0], "m", 1) == 0)
  6544. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_MULTICAST, NULL, 1, BGP_PATH_ALL);
  6545. return bgp_show_route (vty, NULL, argv[1], AFI_IP, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6546. }
  6547. DEFUN (show_bgp_ipv4_vpn_prefix,
  6548. show_bgp_ipv4_vpn_prefix_cmd,
  6549. "show bgp ipv4 vpn A.B.C.D/M",
  6550. SHOW_STR
  6551. BGP_STR
  6552. IP_STR
  6553. "Display VPN NLRI specific information\n"
  6554. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6555. {
  6556. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_MPLS_VPN, NULL, 1, BGP_PATH_ALL);
  6557. }
  6558. DEFUN (show_bgp_ipv6_vpn_prefix,
  6559. show_bgp_ipv6_vpn_prefix_cmd,
  6560. "show bgp ipv6 vpn X:X::X:X/M",
  6561. SHOW_STR
  6562. BGP_STR
  6563. "Address Family\n"
  6564. "Display VPN NLRI specific information\n"
  6565. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6566. {
  6567. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_MPLS_VPN, NULL, 1, BGP_PATH_ALL);
  6568. }
  6569. DEFUN (show_bgp_ipv4_encap_prefix,
  6570. show_bgp_ipv4_encap_prefix_cmd,
  6571. "show bgp ipv4 encap A.B.C.D/M",
  6572. SHOW_STR
  6573. BGP_STR
  6574. IP_STR
  6575. "Display ENCAP NLRI specific information\n"
  6576. "Display information about ENCAP NLRIs\n"
  6577. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6578. {
  6579. return bgp_show_route (vty, NULL, argv[0], AFI_IP, SAFI_ENCAP, NULL, 1, BGP_PATH_ALL);
  6580. }
  6581. DEFUN (show_bgp_ipv6_encap_prefix,
  6582. show_bgp_ipv6_encap_prefix_cmd,
  6583. "show bgp ipv6 encap X:X::X:X/M",
  6584. SHOW_STR
  6585. BGP_STR
  6586. IP_STR
  6587. "Display ENCAP NLRI specific information\n"
  6588. "Display information about ENCAP NLRIs\n"
  6589. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6590. {
  6591. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_ENCAP, NULL, 1, BGP_PATH_ALL);
  6592. }
  6593. DEFUN (show_bgp_ipv4_safi_rd_prefix,
  6594. show_bgp_ipv4_safi_rd_prefix_cmd,
  6595. "show bgp ipv4 (encap|vpn) rd ASN:nn_or_IP-address:nn A.B.C.D/M",
  6596. SHOW_STR
  6597. BGP_STR
  6598. "Address Family\n"
  6599. "Address Family Modifier\n"
  6600. "Address Family Modifier\n"
  6601. "Display information for a route distinguisher\n"
  6602. "ENCAP Route Distinguisher\n"
  6603. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6604. {
  6605. int ret;
  6606. struct prefix_rd prd;
  6607. safi_t safi;
  6608. if (bgp_parse_safi(argv[0], &safi)) {
  6609. vty_out (vty, "Error: Bad SAFI: %s%s", argv[0], VTY_NEWLINE);
  6610. return CMD_WARNING;
  6611. }
  6612. ret = str2prefix_rd (argv[1], &prd);
  6613. if (! ret)
  6614. {
  6615. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6616. return CMD_WARNING;
  6617. }
  6618. return bgp_show_route (vty, NULL, argv[2], AFI_IP, safi, &prd, 1, BGP_PATH_ALL);
  6619. }
  6620. DEFUN (show_bgp_ipv6_safi_rd_prefix,
  6621. show_bgp_ipv6_safi_rd_prefix_cmd,
  6622. "show bgp ipv6 (encap|vpn) rd ASN:nn_or_IP-address:nn X:X::X:X/M",
  6623. SHOW_STR
  6624. BGP_STR
  6625. "Address Family\n"
  6626. "Address Family Modifier\n"
  6627. "Address Family Modifier\n"
  6628. "Display information for a route distinguisher\n"
  6629. "ENCAP Route Distinguisher\n"
  6630. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6631. {
  6632. int ret;
  6633. struct prefix_rd prd;
  6634. safi_t safi;
  6635. if (bgp_parse_safi(argv[0], &safi)) {
  6636. vty_out (vty, "Error: Bad SAFI: %s%s", argv[0], VTY_NEWLINE);
  6637. return CMD_WARNING;
  6638. }
  6639. ret = str2prefix_rd (argv[1], &prd);
  6640. if (! ret)
  6641. {
  6642. vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
  6643. return CMD_WARNING;
  6644. }
  6645. return bgp_show_route (vty, NULL, argv[2], AFI_IP6, safi, &prd, 1, BGP_PATH_ALL);
  6646. }
  6647. DEFUN (show_bgp_afi_safi_view,
  6648. show_bgp_afi_safi_view_cmd,
  6649. "show bgp view WORD (ipv4|ipv6) (encap|mulicast|unicast|vpn)",
  6650. SHOW_STR
  6651. BGP_STR
  6652. "BGP view\n"
  6653. "BGP view name\n"
  6654. "Address Family\n"
  6655. "Address Family\n"
  6656. "Address Family Modifier\n"
  6657. "Address Family Modifier\n"
  6658. "Address Family Modifier\n"
  6659. "Address Family Modifier\n"
  6660. )
  6661. {
  6662. struct bgp *bgp;
  6663. safi_t safi;
  6664. afi_t afi;
  6665. if (bgp_parse_afi(argv[1], &afi)) {
  6666. vty_out (vty, "Error: Bad AFI: %s%s", argv[0], VTY_NEWLINE);
  6667. return CMD_WARNING;
  6668. }
  6669. if (bgp_parse_safi(argv[2], &safi)) {
  6670. vty_out (vty, "Error: Bad SAFI: %s%s", argv[1], VTY_NEWLINE);
  6671. return CMD_WARNING;
  6672. }
  6673. /* BGP structure lookup. */
  6674. bgp = bgp_lookup_by_name (argv[0]);
  6675. if (bgp == NULL)
  6676. {
  6677. vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
  6678. return CMD_WARNING;
  6679. }
  6680. return bgp_show (vty, bgp, afi, safi, bgp_show_type_normal, NULL);
  6681. }
  6682. DEFUN (show_bgp_view_afi_safi_route,
  6683. show_bgp_view_afi_safi_route_cmd,
  6684. "show bgp view WORD (ipv4|ipv6) (encap|multicast|unicast|vpn) A.B.C.D",
  6685. SHOW_STR
  6686. BGP_STR
  6687. "BGP view\n"
  6688. "View name\n"
  6689. "Address Family\n"
  6690. "Address Family\n"
  6691. "Address Family Modifier\n"
  6692. "Address Family Modifier\n"
  6693. "Address Family Modifier\n"
  6694. "Address Family Modifier\n"
  6695. "Network in the BGP routing table to display\n")
  6696. {
  6697. safi_t safi;
  6698. afi_t afi;
  6699. if (bgp_parse_afi(argv[1], &afi)) {
  6700. vty_out (vty, "Error: Bad AFI: %s%s", argv[0], VTY_NEWLINE);
  6701. return CMD_WARNING;
  6702. }
  6703. if (bgp_parse_safi(argv[2], &safi)) {
  6704. vty_out (vty, "Error: Bad SAFI: %s%s", argv[1], VTY_NEWLINE);
  6705. return CMD_WARNING;
  6706. }
  6707. return bgp_show_route (vty, argv[0], argv[3], afi, safi, NULL, 0, BGP_PATH_ALL);
  6708. }
  6709. DEFUN (show_bgp_view_afi_safi_prefix,
  6710. show_bgp_view_afi_safi_prefix_cmd,
  6711. "show bgp view WORD (ipv4|ipv6) (encap|multicast|unicast|vpn) A.B.C.D/M",
  6712. SHOW_STR
  6713. BGP_STR
  6714. "BGP view\n"
  6715. "View name\n"
  6716. "Address Family\n"
  6717. "Address Family\n"
  6718. "Address Family Modifier\n"
  6719. "Address Family Modifier\n"
  6720. "Address Family Modifier\n"
  6721. "Address Family Modifier\n"
  6722. "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
  6723. {
  6724. safi_t safi;
  6725. afi_t afi;
  6726. if (bgp_parse_afi(argv[1], &afi)) {
  6727. vty_out (vty, "Error: Bad AFI: %s%s", argv[0], VTY_NEWLINE);
  6728. return CMD_WARNING;
  6729. }
  6730. if (bgp_parse_safi(argv[2], &safi)) {
  6731. vty_out (vty, "Error: Bad SAFI: %s%s", argv[1], VTY_NEWLINE);
  6732. return CMD_WARNING;
  6733. }
  6734. return bgp_show_route (vty, argv[0], argv[3], afi, safi, NULL, 1, BGP_PATH_ALL);
  6735. }
  6736. /* new001 */
  6737. DEFUN (show_bgp_afi,
  6738. show_bgp_afi_cmd,
  6739. "show bgp (ipv4|ipv6)",
  6740. SHOW_STR
  6741. BGP_STR
  6742. "Address family\n"
  6743. "Address family\n")
  6744. {
  6745. afi_t afi;
  6746. if (bgp_parse_afi(argv[0], &afi)) {
  6747. vty_out (vty, "Error: Bad AFI: %s%s", argv[0], VTY_NEWLINE);
  6748. return CMD_WARNING;
  6749. }
  6750. return bgp_show (vty, NULL, afi, SAFI_UNICAST, bgp_show_type_normal,
  6751. NULL);
  6752. }
  6753. DEFUN (show_bgp_ipv6_safi,
  6754. show_bgp_ipv6_safi_cmd,
  6755. "show bgp ipv6 (unicast|multicast)",
  6756. SHOW_STR
  6757. BGP_STR
  6758. "Address family\n"
  6759. "Address Family modifier\n"
  6760. "Address Family modifier\n")
  6761. {
  6762. if (strncmp (argv[0], "m", 1) == 0)
  6763. return bgp_show (vty, NULL, AFI_IP6, SAFI_MULTICAST, bgp_show_type_normal,
  6764. NULL);
  6765. return bgp_show (vty, NULL, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal, NULL);
  6766. }
  6767. DEFUN (show_bgp_ipv6_route,
  6768. show_bgp_ipv6_route_cmd,
  6769. "show bgp ipv6 X:X::X:X",
  6770. SHOW_STR
  6771. BGP_STR
  6772. "Address family\n"
  6773. "Network in the BGP routing table to display\n")
  6774. {
  6775. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6776. }
  6777. DEFUN (show_bgp_ipv6_safi_route,
  6778. show_bgp_ipv6_safi_route_cmd,
  6779. "show bgp ipv6 (unicast|multicast) X:X::X:X",
  6780. SHOW_STR
  6781. BGP_STR
  6782. "Address family\n"
  6783. "Address Family modifier\n"
  6784. "Address Family modifier\n"
  6785. "Network in the BGP routing table to display\n")
  6786. {
  6787. if (strncmp (argv[0], "m", 1) == 0)
  6788. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 0, BGP_PATH_ALL);
  6789. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6790. }
  6791. /* old command */
  6792. DEFUN (show_ipv6_bgp_route,
  6793. show_ipv6_bgp_route_cmd,
  6794. "show ipv6 bgp X:X::X:X",
  6795. SHOW_STR
  6796. IP_STR
  6797. BGP_STR
  6798. "Network in the BGP routing table to display\n")
  6799. {
  6800. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6801. }
  6802. DEFUN (show_bgp_prefix,
  6803. show_bgp_prefix_cmd,
  6804. "show bgp X:X::X:X/M",
  6805. SHOW_STR
  6806. BGP_STR
  6807. "IPv6 prefix <network>/<length>\n")
  6808. {
  6809. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6810. }
  6811. /* new002 */
  6812. DEFUN (show_bgp_ipv6_prefix,
  6813. show_bgp_ipv6_prefix_cmd,
  6814. "show bgp ipv6 X:X::X:X/M",
  6815. SHOW_STR
  6816. BGP_STR
  6817. "Address family\n"
  6818. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
  6819. {
  6820. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6821. }
  6822. DEFUN (show_bgp_ipv6_safi_prefix,
  6823. show_bgp_ipv6_safi_prefix_cmd,
  6824. "show bgp ipv6 (unicast|multicast) X:X::X:X/M",
  6825. SHOW_STR
  6826. BGP_STR
  6827. "Address family\n"
  6828. "Address Family modifier\n"
  6829. "Address Family modifier\n"
  6830. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
  6831. {
  6832. if (strncmp (argv[0], "m", 1) == 0)
  6833. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_MULTICAST, NULL, 1, BGP_PATH_ALL);
  6834. return bgp_show_route (vty, NULL, argv[1], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6835. }
  6836. /* old command */
  6837. DEFUN (show_ipv6_bgp_prefix,
  6838. show_ipv6_bgp_prefix_cmd,
  6839. "show ipv6 bgp X:X::X:X/M",
  6840. SHOW_STR
  6841. IP_STR
  6842. BGP_STR
  6843. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
  6844. {
  6845. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_UNICAST, NULL, 1, BGP_PATH_ALL);
  6846. }
  6847. DEFUN (show_bgp_view,
  6848. show_bgp_view_cmd,
  6849. "show bgp view WORD",
  6850. SHOW_STR
  6851. BGP_STR
  6852. "BGP view\n"
  6853. "View name\n")
  6854. {
  6855. struct bgp *bgp;
  6856. /* BGP structure lookup. */
  6857. bgp = bgp_lookup_by_name (argv[0]);
  6858. if (bgp == NULL)
  6859. {
  6860. vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
  6861. return CMD_WARNING;
  6862. }
  6863. return bgp_show (vty, bgp, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal, NULL);
  6864. }
  6865. DEFUN (show_bgp_view_ipv6,
  6866. show_bgp_view_ipv6_cmd,
  6867. "show bgp view WORD ipv6",
  6868. SHOW_STR
  6869. BGP_STR
  6870. "BGP view\n"
  6871. "View name\n"
  6872. "Address family\n")
  6873. {
  6874. struct bgp *bgp;
  6875. /* BGP structure lookup. */
  6876. bgp = bgp_lookup_by_name (argv[0]);
  6877. if (bgp == NULL)
  6878. {
  6879. vty_out (vty, "Can't find BGP view %s%s", argv[0], VTY_NEWLINE);
  6880. return CMD_WARNING;
  6881. }
  6882. return bgp_show (vty, bgp, AFI_IP6, SAFI_UNICAST, bgp_show_type_normal, NULL);
  6883. }
  6884. DEFUN (show_bgp_view_route,
  6885. show_bgp_view_route_cmd,
  6886. "show bgp view WORD X:X::X:X",
  6887. SHOW_STR
  6888. BGP_STR
  6889. "BGP view\n"
  6890. "View name\n"
  6891. "Network in the BGP routing table to display\n")
  6892. {
  6893. return bgp_show_route (vty, argv[0], argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6894. }
  6895. DEFUN (show_bgp_view_ipv6_route,
  6896. show_bgp_view_ipv6_route_cmd,
  6897. "show bgp view WORD ipv6 X:X::X:X",
  6898. SHOW_STR
  6899. BGP_STR
  6900. "BGP view\n"
  6901. "View name\n"
  6902. "Address family\n"
  6903. "Network in the BGP routing table to display\n")
  6904. {
  6905. return bgp_show_route (vty, argv[0], argv[1], AFI_IP6, SAFI_UNICAST, NULL, 0, BGP_PATH_ALL);
  6906. }
  6907. /* old command */
  6908. DEFUN (show_ipv6_mbgp,
  6909. show_ipv6_mbgp_cmd,
  6910. "show ipv6 mbgp",
  6911. SHOW_STR
  6912. IP_STR
  6913. MBGP_STR)
  6914. {
  6915. return bgp_show (vty, NULL, AFI_IP6, SAFI_MULTICAST, bgp_show_type_normal,
  6916. NULL);
  6917. }
  6918. /* old command */
  6919. DEFUN (show_ipv6_mbgp_route,
  6920. show_ipv6_mbgp_route_cmd,
  6921. "show ipv6 mbgp X:X::X:X",
  6922. SHOW_STR
  6923. IP_STR
  6924. MBGP_STR
  6925. "Network in the MBGP routing table to display\n")
  6926. {
  6927. return bgp_show_route (vty, NULL, argv[0], AFI_IP6, SAFI_MULTICAST, NULL, 0, BGP_PATH_ALL);
  6928. }
  6929. /* old command */
  6930. DEFUN (show_ipv6_mbgp_prefix,
  6931. show_ipv6_mbgp_prefix_cmd,
  6932. "show ipv6 mbgp X:X::X:X/M",
  6933. SHOW_STR
  6934. IP_STR
  6935. MBGP_STR
  6936. "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")