bgp_route.c 527 KB

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