bgp_route.c 517 KB

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