bgp_route.c 516 KB

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