bgpd.c 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615
  1. /* BGP-4, BGP-4+ daemon program
  2. Copyright (C) 1996, 97, 98, 99, 2000 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 "thread.h"
  19. #include "buffer.h"
  20. #include "stream.h"
  21. #include "command.h"
  22. #include "sockunion.h"
  23. #include "sockopt.h"
  24. #include "network.h"
  25. #include "memory.h"
  26. #include "filter.h"
  27. #include "routemap.h"
  28. #include "str.h"
  29. #include "log.h"
  30. #include "plist.h"
  31. #include "linklist.h"
  32. #include "workqueue.h"
  33. #include "table.h"
  34. #include "bgpd/bgpd.h"
  35. #include "bgpd/bgp_table.h"
  36. #include "bgpd/bgp_aspath.h"
  37. #include "bgpd/bgp_route.h"
  38. #include "bgpd/bgp_dump.h"
  39. #include "bgpd/bgp_debug.h"
  40. #include "bgpd/bgp_community.h"
  41. #include "bgpd/bgp_attr.h"
  42. #include "bgpd/bgp_regex.h"
  43. #include "bgpd/bgp_clist.h"
  44. #include "bgpd/bgp_fsm.h"
  45. #include "bgpd/bgp_packet.h"
  46. #include "bgpd/bgp_zebra.h"
  47. #include "bgpd/bgp_open.h"
  48. #include "bgpd/bgp_filter.h"
  49. #include "bgpd/bgp_nexthop.h"
  50. #include "bgpd/bgp_damp.h"
  51. #include "bgpd/bgp_mplsvpn.h"
  52. #include "bgpd/bgp_encap.h"
  53. #include "bgpd/bgp_advertise.h"
  54. #include "bgpd/bgp_network.h"
  55. #include "bgpd/bgp_vty.h"
  56. #include "bgpd/bgp_mpath.h"
  57. #include "bgpd/bgp_nht.h"
  58. #ifdef HAVE_SNMP
  59. #include "bgpd/bgp_snmp.h"
  60. #endif /* HAVE_SNMP */
  61. /* BGP process wide configuration. */
  62. static struct bgp_master bgp_master;
  63. extern struct in_addr router_id_zebra;
  64. /* BGP process wide configuration pointer to export. */
  65. struct bgp_master *bm;
  66. /* BGP community-list. */
  67. struct community_list_handler *bgp_clist;
  68. /* BGP global flag manipulation. */
  69. int
  70. bgp_option_set (int flag)
  71. {
  72. switch (flag)
  73. {
  74. case BGP_OPT_NO_FIB:
  75. case BGP_OPT_MULTIPLE_INSTANCE:
  76. case BGP_OPT_CONFIG_CISCO:
  77. case BGP_OPT_NO_LISTEN:
  78. SET_FLAG (bm->options, flag);
  79. break;
  80. default:
  81. return BGP_ERR_INVALID_FLAG;
  82. }
  83. return 0;
  84. }
  85. int
  86. bgp_option_unset (int flag)
  87. {
  88. switch (flag)
  89. {
  90. case BGP_OPT_MULTIPLE_INSTANCE:
  91. if (listcount (bm->bgp) > 1)
  92. return BGP_ERR_MULTIPLE_INSTANCE_USED;
  93. /* Fall through. */
  94. case BGP_OPT_NO_FIB:
  95. case BGP_OPT_CONFIG_CISCO:
  96. UNSET_FLAG (bm->options, flag);
  97. break;
  98. default:
  99. return BGP_ERR_INVALID_FLAG;
  100. }
  101. return 0;
  102. }
  103. int
  104. bgp_option_check (int flag)
  105. {
  106. return CHECK_FLAG (bm->options, flag);
  107. }
  108. /* BGP flag manipulation. */
  109. int
  110. bgp_flag_set (struct bgp *bgp, int flag)
  111. {
  112. SET_FLAG (bgp->flags, flag);
  113. return 0;
  114. }
  115. int
  116. bgp_flag_unset (struct bgp *bgp, int flag)
  117. {
  118. UNSET_FLAG (bgp->flags, flag);
  119. return 0;
  120. }
  121. int
  122. bgp_flag_check (struct bgp *bgp, int flag)
  123. {
  124. return CHECK_FLAG (bgp->flags, flag);
  125. }
  126. /* Internal function to set BGP structure configureation flag. */
  127. static void
  128. bgp_config_set (struct bgp *bgp, int config)
  129. {
  130. SET_FLAG (bgp->config, config);
  131. }
  132. static void
  133. bgp_config_unset (struct bgp *bgp, int config)
  134. {
  135. UNSET_FLAG (bgp->config, config);
  136. }
  137. static int
  138. bgp_config_check (struct bgp *bgp, int config)
  139. {
  140. return CHECK_FLAG (bgp->config, config);
  141. }
  142. /* Set BGP router identifier. */
  143. static int
  144. bgp_router_id_set (struct bgp *bgp, struct in_addr *id)
  145. {
  146. struct peer *peer;
  147. struct listnode *node, *nnode;
  148. if (bgp_config_check (bgp, BGP_CONFIG_ROUTER_ID)
  149. && IPV4_ADDR_SAME (&bgp->router_id, id))
  150. return 0;
  151. IPV4_ADDR_COPY (&bgp->router_id, id);
  152. bgp_config_set (bgp, BGP_CONFIG_ROUTER_ID);
  153. /* Set all peer's local identifier with this value. */
  154. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  155. {
  156. IPV4_ADDR_COPY (&peer->local_id, id);
  157. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  158. {
  159. peer->last_reset = PEER_DOWN_RID_CHANGE;
  160. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  161. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  162. }
  163. }
  164. return 0;
  165. }
  166. void
  167. bgp_router_id_zebra_bump (void)
  168. {
  169. struct listnode *node, *nnode;
  170. struct bgp *bgp;
  171. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  172. {
  173. if (!bgp->router_id_static.s_addr)
  174. bgp_router_id_set (bgp, &router_id_zebra);
  175. }
  176. }
  177. int
  178. bgp_router_id_static_set (struct bgp *bgp, struct in_addr id)
  179. {
  180. bgp->router_id_static = id;
  181. bgp_router_id_set (bgp, id.s_addr ? &id : &router_id_zebra);
  182. return 0;
  183. }
  184. /* BGP's cluster-id control. */
  185. int
  186. bgp_cluster_id_set (struct bgp *bgp, struct in_addr *cluster_id)
  187. {
  188. struct peer *peer;
  189. struct listnode *node, *nnode;
  190. if (bgp_config_check (bgp, BGP_CONFIG_CLUSTER_ID)
  191. && IPV4_ADDR_SAME (&bgp->cluster_id, cluster_id))
  192. return 0;
  193. IPV4_ADDR_COPY (&bgp->cluster_id, cluster_id);
  194. bgp_config_set (bgp, BGP_CONFIG_CLUSTER_ID);
  195. /* Clear all IBGP peer. */
  196. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  197. {
  198. if (peer->sort != BGP_PEER_IBGP)
  199. continue;
  200. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  201. {
  202. peer->last_reset = PEER_DOWN_CLID_CHANGE;
  203. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  204. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  205. }
  206. }
  207. return 0;
  208. }
  209. int
  210. bgp_cluster_id_unset (struct bgp *bgp)
  211. {
  212. struct peer *peer;
  213. struct listnode *node, *nnode;
  214. if (! bgp_config_check (bgp, BGP_CONFIG_CLUSTER_ID))
  215. return 0;
  216. bgp->cluster_id.s_addr = 0;
  217. bgp_config_unset (bgp, BGP_CONFIG_CLUSTER_ID);
  218. /* Clear all IBGP peer. */
  219. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  220. {
  221. if (peer->sort != BGP_PEER_IBGP)
  222. continue;
  223. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  224. {
  225. peer->last_reset = PEER_DOWN_CLID_CHANGE;
  226. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  227. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  228. }
  229. }
  230. return 0;
  231. }
  232. /* time_t value that is monotonicly increasing
  233. * and uneffected by adjustments to system clock
  234. */
  235. time_t bgp_clock (void)
  236. {
  237. struct timeval tv;
  238. quagga_gettime(QUAGGA_CLK_MONOTONIC, &tv);
  239. return tv.tv_sec;
  240. }
  241. /* BGP timer configuration. */
  242. int
  243. bgp_timers_set (struct bgp *bgp, u_int32_t keepalive, u_int32_t holdtime)
  244. {
  245. bgp->default_keepalive = (keepalive < holdtime / 3
  246. ? keepalive : holdtime / 3);
  247. bgp->default_holdtime = holdtime;
  248. return 0;
  249. }
  250. int
  251. bgp_timers_unset (struct bgp *bgp)
  252. {
  253. bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
  254. bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
  255. return 0;
  256. }
  257. /* BGP confederation configuration. */
  258. int
  259. bgp_confederation_id_set (struct bgp *bgp, as_t as)
  260. {
  261. struct peer *peer;
  262. struct listnode *node, *nnode;
  263. int already_confed;
  264. if (as == 0)
  265. return BGP_ERR_INVALID_AS;
  266. /* Remember - were we doing confederation before? */
  267. already_confed = bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION);
  268. bgp->confed_id = as;
  269. bgp_config_set (bgp, BGP_CONFIG_CONFEDERATION);
  270. /* If we were doing confederation already, this is just an external
  271. AS change. Just Reset EBGP sessions, not CONFED sessions. If we
  272. were not doing confederation before, reset all EBGP sessions. */
  273. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  274. {
  275. /* We're looking for peers who's AS is not local or part of our
  276. confederation. */
  277. if (already_confed)
  278. {
  279. if (peer_sort (peer) == BGP_PEER_EBGP)
  280. {
  281. peer->local_as = as;
  282. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  283. {
  284. peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE;
  285. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  286. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  287. }
  288. else
  289. BGP_EVENT_ADD (peer, BGP_Stop);
  290. }
  291. }
  292. else
  293. {
  294. /* Not doign confederation before, so reset every non-local
  295. session */
  296. if (peer_sort (peer) != BGP_PEER_IBGP)
  297. {
  298. /* Reset the local_as to be our EBGP one */
  299. if (peer_sort (peer) == BGP_PEER_EBGP)
  300. peer->local_as = as;
  301. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  302. {
  303. peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE;
  304. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  305. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  306. }
  307. else
  308. BGP_EVENT_ADD (peer, BGP_Stop);
  309. }
  310. }
  311. }
  312. return 0;
  313. }
  314. int
  315. bgp_confederation_id_unset (struct bgp *bgp)
  316. {
  317. struct peer *peer;
  318. struct listnode *node, *nnode;
  319. bgp->confed_id = 0;
  320. bgp_config_unset (bgp, BGP_CONFIG_CONFEDERATION);
  321. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  322. {
  323. /* We're looking for peers who's AS is not local */
  324. if (peer_sort (peer) != BGP_PEER_IBGP)
  325. {
  326. peer->local_as = bgp->as;
  327. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  328. {
  329. peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE;
  330. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  331. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  332. }
  333. else
  334. BGP_EVENT_ADD (peer, BGP_Stop);
  335. }
  336. }
  337. return 0;
  338. }
  339. /* Is an AS part of the confed or not? */
  340. int
  341. bgp_confederation_peers_check (struct bgp *bgp, as_t as)
  342. {
  343. int i;
  344. if (! bgp)
  345. return 0;
  346. for (i = 0; i < bgp->confed_peers_cnt; i++)
  347. if (bgp->confed_peers[i] == as)
  348. return 1;
  349. return 0;
  350. }
  351. /* Add an AS to the confederation set. */
  352. int
  353. bgp_confederation_peers_add (struct bgp *bgp, as_t as)
  354. {
  355. struct peer *peer;
  356. struct listnode *node, *nnode;
  357. if (! bgp)
  358. return BGP_ERR_INVALID_BGP;
  359. if (bgp->as == as)
  360. return BGP_ERR_INVALID_AS;
  361. if (bgp_confederation_peers_check (bgp, as))
  362. return -1;
  363. if (bgp->confed_peers)
  364. bgp->confed_peers = XREALLOC (MTYPE_BGP_CONFED_LIST,
  365. bgp->confed_peers,
  366. (bgp->confed_peers_cnt + 1) * sizeof (as_t));
  367. else
  368. bgp->confed_peers = XMALLOC (MTYPE_BGP_CONFED_LIST,
  369. (bgp->confed_peers_cnt + 1) * sizeof (as_t));
  370. bgp->confed_peers[bgp->confed_peers_cnt] = as;
  371. bgp->confed_peers_cnt++;
  372. if (bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION))
  373. {
  374. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  375. {
  376. if (peer->as == as)
  377. {
  378. peer->local_as = bgp->as;
  379. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  380. {
  381. peer->last_reset = PEER_DOWN_CONFED_PEER_CHANGE;
  382. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  383. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  384. }
  385. else
  386. BGP_EVENT_ADD (peer, BGP_Stop);
  387. }
  388. }
  389. }
  390. return 0;
  391. }
  392. /* Delete an AS from the confederation set. */
  393. int
  394. bgp_confederation_peers_remove (struct bgp *bgp, as_t as)
  395. {
  396. int i;
  397. int j;
  398. struct peer *peer;
  399. struct listnode *node, *nnode;
  400. if (! bgp)
  401. return -1;
  402. if (! bgp_confederation_peers_check (bgp, as))
  403. return -1;
  404. for (i = 0; i < bgp->confed_peers_cnt; i++)
  405. if (bgp->confed_peers[i] == as)
  406. for(j = i + 1; j < bgp->confed_peers_cnt; j++)
  407. bgp->confed_peers[j - 1] = bgp->confed_peers[j];
  408. bgp->confed_peers_cnt--;
  409. if (bgp->confed_peers_cnt == 0)
  410. {
  411. if (bgp->confed_peers)
  412. XFREE (MTYPE_BGP_CONFED_LIST, bgp->confed_peers);
  413. bgp->confed_peers = NULL;
  414. }
  415. else
  416. bgp->confed_peers = XREALLOC (MTYPE_BGP_CONFED_LIST,
  417. bgp->confed_peers,
  418. bgp->confed_peers_cnt * sizeof (as_t));
  419. /* Now reset any peer who's remote AS has just been removed from the
  420. CONFED */
  421. if (bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION))
  422. {
  423. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  424. {
  425. if (peer->as == as)
  426. {
  427. peer->local_as = bgp->confed_id;
  428. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  429. {
  430. peer->last_reset = PEER_DOWN_CONFED_PEER_CHANGE;
  431. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  432. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  433. }
  434. else
  435. BGP_EVENT_ADD (peer, BGP_Stop);
  436. }
  437. }
  438. }
  439. return 0;
  440. }
  441. /* Local preference configuration. */
  442. int
  443. bgp_default_local_preference_set (struct bgp *bgp, u_int32_t local_pref)
  444. {
  445. if (! bgp)
  446. return -1;
  447. bgp->default_local_pref = local_pref;
  448. return 0;
  449. }
  450. int
  451. bgp_default_local_preference_unset (struct bgp *bgp)
  452. {
  453. if (! bgp)
  454. return -1;
  455. bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
  456. return 0;
  457. }
  458. /* If peer is RSERVER_CLIENT in at least one address family and is not member
  459. of a peer_group for that family, return 1.
  460. Used to check wether the peer is included in list bgp->rsclient. */
  461. int
  462. peer_rsclient_active (struct peer *peer)
  463. {
  464. int i;
  465. int j;
  466. for (i=AFI_IP; i < AFI_MAX; i++)
  467. for (j=SAFI_UNICAST; j < SAFI_MAX; j++)
  468. if (CHECK_FLAG(peer->af_flags[i][j], PEER_FLAG_RSERVER_CLIENT)
  469. && ! peer->af_group[i][j])
  470. return 1;
  471. return 0;
  472. }
  473. /* Peer comparison function for sorting. */
  474. static int
  475. peer_cmp (struct peer *p1, struct peer *p2)
  476. {
  477. return sockunion_cmp (&p1->su, &p2->su);
  478. }
  479. int
  480. peer_af_flag_check (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag)
  481. {
  482. return CHECK_FLAG (peer->af_flags[afi][safi], flag);
  483. }
  484. /* Reset all address family specific configuration. */
  485. static void
  486. peer_af_flag_reset (struct peer *peer, afi_t afi, safi_t safi)
  487. {
  488. int i;
  489. struct bgp_filter *filter;
  490. char orf_name[BUFSIZ];
  491. filter = &peer->filter[afi][safi];
  492. /* Clear neighbor filter and route-map */
  493. for (i = FILTER_IN; i < FILTER_MAX; i++)
  494. {
  495. if (filter->dlist[i].name)
  496. {
  497. free (filter->dlist[i].name);
  498. filter->dlist[i].name = NULL;
  499. }
  500. if (filter->plist[i].name)
  501. {
  502. free (filter->plist[i].name);
  503. filter->plist[i].name = NULL;
  504. }
  505. if (filter->aslist[i].name)
  506. {
  507. free (filter->aslist[i].name);
  508. filter->aslist[i].name = NULL;
  509. }
  510. }
  511. for (i = RMAP_IN; i < RMAP_MAX; i++)
  512. {
  513. if (filter->map[i].name)
  514. {
  515. free (filter->map[i].name);
  516. filter->map[i].name = NULL;
  517. }
  518. }
  519. /* Clear unsuppress map. */
  520. if (filter->usmap.name)
  521. free (filter->usmap.name);
  522. filter->usmap.name = NULL;
  523. filter->usmap.map = NULL;
  524. /* Clear neighbor's all address family flags. */
  525. peer->af_flags[afi][safi] = 0;
  526. /* Clear neighbor's all address family sflags. */
  527. peer->af_sflags[afi][safi] = 0;
  528. /* Clear neighbor's all address family capabilities. */
  529. peer->af_cap[afi][safi] = 0;
  530. /* Clear ORF info */
  531. peer->orf_plist[afi][safi] = NULL;
  532. sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
  533. prefix_bgp_orf_remove_all (afi, orf_name);
  534. /* Set default neighbor send-community. */
  535. if (! bgp_option_check (BGP_OPT_CONFIG_CISCO))
  536. {
  537. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY);
  538. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY);
  539. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_LARGE_COMMUNITY);
  540. }
  541. /* Clear neighbor default_originate_rmap */
  542. if (peer->default_rmap[afi][safi].name)
  543. free (peer->default_rmap[afi][safi].name);
  544. peer->default_rmap[afi][safi].name = NULL;
  545. peer->default_rmap[afi][safi].map = NULL;
  546. /* Clear neighbor maximum-prefix */
  547. peer->pmax[afi][safi] = 0;
  548. peer->pmax_threshold[afi][safi] = MAXIMUM_PREFIX_THRESHOLD_DEFAULT;
  549. }
  550. /* peer global config reset */
  551. static void
  552. peer_global_config_reset (struct peer *peer)
  553. {
  554. peer->weight = 0;
  555. peer->change_local_as = 0;
  556. peer->ttl = 0;
  557. peer->gtsm_hops = 0;
  558. if (peer->update_source)
  559. {
  560. sockunion_free (peer->update_source);
  561. peer->update_source = NULL;
  562. }
  563. if (peer->update_if)
  564. {
  565. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  566. peer->update_if = NULL;
  567. }
  568. if (peer_sort (peer) == BGP_PEER_IBGP)
  569. peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  570. else
  571. peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  572. peer->flags = 0;
  573. peer->config = 0;
  574. peer->holdtime = 0;
  575. peer->keepalive = 0;
  576. peer->connect = 0;
  577. peer->v_connect = BGP_DEFAULT_CONNECT_RETRY;
  578. }
  579. /* Check peer's AS number and determines if this peer is IBGP or EBGP */
  580. static bgp_peer_sort_t
  581. peer_calc_sort (struct peer *peer)
  582. {
  583. struct bgp *bgp;
  584. bgp = peer->bgp;
  585. /* Peer-group */
  586. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  587. {
  588. if (peer->as)
  589. return (bgp->as == peer->as ? BGP_PEER_IBGP : BGP_PEER_EBGP);
  590. else
  591. {
  592. struct peer *peer1;
  593. peer1 = listnode_head (peer->group->peer);
  594. if (peer1)
  595. return (peer1->local_as == peer1->as
  596. ? BGP_PEER_IBGP : BGP_PEER_EBGP);
  597. }
  598. return BGP_PEER_INTERNAL;
  599. }
  600. /* Normal peer */
  601. if (bgp && CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
  602. {
  603. if (peer->local_as == 0)
  604. return BGP_PEER_INTERNAL;
  605. if (peer->local_as == peer->as)
  606. {
  607. if (peer->local_as == bgp->confed_id)
  608. return BGP_PEER_EBGP;
  609. else
  610. return BGP_PEER_IBGP;
  611. }
  612. if (bgp_confederation_peers_check (bgp, peer->as))
  613. return BGP_PEER_CONFED;
  614. return BGP_PEER_EBGP;
  615. }
  616. else
  617. {
  618. return (peer->local_as == 0
  619. ? BGP_PEER_INTERNAL : peer->local_as == peer->as
  620. ? BGP_PEER_IBGP : BGP_PEER_EBGP);
  621. }
  622. }
  623. /* Calculate and cache the peer "sort" */
  624. bgp_peer_sort_t
  625. peer_sort (struct peer *peer)
  626. {
  627. peer->sort = peer_calc_sort (peer);
  628. return peer->sort;
  629. }
  630. static void
  631. peer_free (struct peer *peer)
  632. {
  633. assert (peer->status == Deleted);
  634. /* this /ought/ to have been done already through bgp_stop earlier,
  635. * but just to be sure..
  636. */
  637. bgp_timer_set (peer);
  638. BGP_READ_OFF (peer->t_read);
  639. BGP_WRITE_OFF (peer->t_write);
  640. BGP_EVENT_FLUSH (peer);
  641. if (peer->desc)
  642. {
  643. XFREE (MTYPE_PEER_DESC, peer->desc);
  644. peer->desc = NULL;
  645. }
  646. /* Free allocated host character. */
  647. if (peer->host)
  648. {
  649. XFREE (MTYPE_BGP_PEER_HOST, peer->host);
  650. peer->host = NULL;
  651. }
  652. /* Update source configuration. */
  653. if (peer->update_source)
  654. {
  655. sockunion_free (peer->update_source);
  656. peer->update_source = NULL;
  657. }
  658. if (peer->update_if)
  659. {
  660. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  661. peer->update_if = NULL;
  662. }
  663. if (peer->clear_node_queue)
  664. {
  665. work_queue_free(peer->clear_node_queue);
  666. peer->clear_node_queue = NULL;
  667. }
  668. if (peer->notify.data)
  669. XFREE(MTYPE_TMP, peer->notify.data);
  670. bgp_sync_delete (peer);
  671. bgp_unlock(peer->bgp);
  672. memset (peer, 0, sizeof (struct peer));
  673. XFREE (MTYPE_BGP_PEER, peer);
  674. }
  675. /* increase reference count on a struct peer */
  676. struct peer *
  677. peer_lock_with_caller (const char *name, struct peer *peer)
  678. {
  679. assert (peer && (peer->lock >= 0));
  680. #if 0
  681. zlog_debug("%s peer_lock %p %d", name, peer, peer->lock);
  682. #endif
  683. peer->lock++;
  684. return peer;
  685. }
  686. /* decrease reference count on a struct peer
  687. * struct peer is freed and NULL returned if last reference
  688. */
  689. struct peer *
  690. peer_unlock_with_caller (const char *name, struct peer *peer)
  691. {
  692. assert (peer && (peer->lock > 0));
  693. #if 0
  694. zlog_debug("%s peer_unlock %p %d", name, peer, peer->lock);
  695. #endif
  696. peer->lock--;
  697. if (peer->lock == 0)
  698. {
  699. peer_free (peer);
  700. return NULL;
  701. }
  702. return peer;
  703. }
  704. /* Allocate new peer object, implicitely locked. */
  705. static struct peer *
  706. peer_new (struct bgp *bgp)
  707. {
  708. afi_t afi;
  709. safi_t safi;
  710. struct peer *peer;
  711. struct servent *sp;
  712. /* bgp argument is absolutely required */
  713. assert (bgp);
  714. if (!bgp)
  715. return NULL;
  716. /* Allocate new peer. */
  717. peer = XCALLOC (MTYPE_BGP_PEER, sizeof (struct peer));
  718. /* Set default value. */
  719. peer->fd = -1;
  720. peer->v_start = BGP_INIT_START_TIMER;
  721. peer->v_connect = BGP_DEFAULT_CONNECT_RETRY;
  722. peer->status = Idle;
  723. peer->ostatus = Idle;
  724. peer->weight = 0;
  725. peer->password = NULL;
  726. peer->bgp = bgp;
  727. peer = peer_lock (peer); /* initial reference */
  728. bgp_lock (bgp);
  729. /* Set default flags. */
  730. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  731. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  732. {
  733. if (! bgp_option_check (BGP_OPT_CONFIG_CISCO))
  734. {
  735. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY);
  736. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY);
  737. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_LARGE_COMMUNITY);
  738. }
  739. peer->orf_plist[afi][safi] = NULL;
  740. }
  741. SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
  742. /* Create buffers. */
  743. peer->ibuf = stream_new (BGP_MAX_PACKET_SIZE);
  744. peer->obuf = stream_fifo_new ();
  745. /* We use a larger buffer for peer->work in the event that:
  746. * - We RX a BGP_UPDATE where the attributes alone are just
  747. * under BGP_MAX_PACKET_SIZE
  748. * - The user configures an outbound route-map that does many as-path
  749. * prepends or adds many communities. At most they can have CMD_ARGC_MAX
  750. * args in a route-map so there is a finite limit on how large they can
  751. * make the attributes.
  752. *
  753. * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid bounds
  754. * checking for every single attribute as we construct an UPDATE.
  755. */
  756. peer->work = stream_new (BGP_MAX_PACKET_SIZE + BGP_MAX_PACKET_SIZE_OVERFLOW);
  757. peer->scratch = stream_new (BGP_MAX_PACKET_SIZE);
  758. bgp_sync_init (peer);
  759. /* Get service port number. */
  760. sp = getservbyname ("bgp", "tcp");
  761. peer->port = (sp == NULL) ? BGP_PORT_DEFAULT : ntohs (sp->s_port);
  762. return peer;
  763. }
  764. /* Create new BGP peer. */
  765. static struct peer *
  766. peer_create (union sockunion *su, struct bgp *bgp, as_t local_as,
  767. as_t remote_as, afi_t afi, safi_t safi)
  768. {
  769. int active;
  770. struct peer *peer;
  771. char buf[SU_ADDRSTRLEN];
  772. peer = peer_new (bgp);
  773. peer->su = *su;
  774. peer->local_as = local_as;
  775. peer->as = remote_as;
  776. peer->local_id = bgp->router_id;
  777. peer->v_holdtime = bgp->default_holdtime;
  778. peer->v_keepalive = bgp->default_keepalive;
  779. if (peer_sort (peer) == BGP_PEER_IBGP)
  780. peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  781. else
  782. peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  783. peer = peer_lock (peer); /* bgp peer list reference */
  784. listnode_add_sort (bgp->peer, peer);
  785. active = peer_active (peer);
  786. if (afi && safi)
  787. peer->afc[afi][safi] = 1;
  788. /* Last read and reset time set */
  789. peer->readtime = peer->resettime = bgp_clock ();
  790. /* Make peer's address string. */
  791. sockunion2str (su, buf, SU_ADDRSTRLEN);
  792. peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
  793. /* Set up peer's events and timers. */
  794. if (! active && peer_active (peer))
  795. bgp_timer_set (peer);
  796. return peer;
  797. }
  798. /* Make accept BGP peer. Called from bgp_accept (). */
  799. struct peer *
  800. peer_create_accept (struct bgp *bgp)
  801. {
  802. struct peer *peer;
  803. peer = peer_new (bgp);
  804. peer = peer_lock (peer); /* bgp peer list reference */
  805. listnode_add_sort (bgp->peer, peer);
  806. return peer;
  807. }
  808. /* Change peer's AS number. */
  809. static void
  810. peer_as_change (struct peer *peer, as_t as)
  811. {
  812. struct peer *conf;
  813. /* Stop peer. */
  814. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  815. {
  816. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  817. {
  818. peer->last_reset = PEER_DOWN_REMOTE_AS_CHANGE;
  819. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  820. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  821. }
  822. else
  823. BGP_EVENT_ADD (peer, BGP_Stop);
  824. }
  825. peer->as = as;
  826. if (bgp_config_check (peer->bgp, BGP_CONFIG_CONFEDERATION)
  827. && ! bgp_confederation_peers_check (peer->bgp, as)
  828. && peer->bgp->as != as)
  829. peer->local_as = peer->bgp->confed_id;
  830. else
  831. peer->local_as = peer->bgp->as;
  832. /* Advertisement-interval reset */
  833. conf = NULL;
  834. if (peer->group)
  835. conf = peer->group->conf;
  836. if (conf && CHECK_FLAG (conf->config, PEER_CONFIG_ROUTEADV))
  837. peer->v_routeadv = conf->routeadv;
  838. else
  839. if (peer_sort (peer) == BGP_PEER_IBGP)
  840. peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  841. else
  842. peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  843. /* reflector-client reset */
  844. if (peer_sort (peer) != BGP_PEER_IBGP)
  845. {
  846. UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST],
  847. PEER_FLAG_REFLECTOR_CLIENT);
  848. UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_MULTICAST],
  849. PEER_FLAG_REFLECTOR_CLIENT);
  850. UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_MPLS_VPN],
  851. PEER_FLAG_REFLECTOR_CLIENT);
  852. UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_ENCAP],
  853. PEER_FLAG_REFLECTOR_CLIENT);
  854. UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_UNICAST],
  855. PEER_FLAG_REFLECTOR_CLIENT);
  856. UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_MULTICAST],
  857. PEER_FLAG_REFLECTOR_CLIENT);
  858. UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_MPLS_VPN],
  859. PEER_FLAG_REFLECTOR_CLIENT);
  860. UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_ENCAP],
  861. PEER_FLAG_REFLECTOR_CLIENT);
  862. }
  863. /* local-as reset */
  864. if (peer_sort (peer) != BGP_PEER_EBGP)
  865. {
  866. peer->change_local_as = 0;
  867. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  868. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  869. }
  870. }
  871. /* If peer does not exist, create new one. If peer already exists,
  872. set AS number to the peer. */
  873. int
  874. peer_remote_as (struct bgp *bgp, union sockunion *su, as_t *as,
  875. afi_t afi, safi_t safi)
  876. {
  877. struct peer *peer;
  878. as_t local_as;
  879. peer = peer_lookup (bgp, su);
  880. if (peer)
  881. {
  882. /* When this peer is a member of peer-group. */
  883. if (peer->group)
  884. {
  885. if (peer->group->conf->as)
  886. {
  887. /* Return peer group's AS number. */
  888. *as = peer->group->conf->as;
  889. return BGP_ERR_PEER_GROUP_MEMBER;
  890. }
  891. if (peer_sort (peer->group->conf) == BGP_PEER_IBGP)
  892. {
  893. if (bgp->as != *as)
  894. {
  895. *as = peer->as;
  896. return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
  897. }
  898. }
  899. else
  900. {
  901. if (bgp->as == *as)
  902. {
  903. *as = peer->as;
  904. return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
  905. }
  906. }
  907. }
  908. /* Existing peer's AS number change. */
  909. if (peer->as != *as)
  910. peer_as_change (peer, *as);
  911. }
  912. else
  913. {
  914. /* If the peer is not part of our confederation, and its not an
  915. iBGP peer then spoof the source AS */
  916. if (bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION)
  917. && ! bgp_confederation_peers_check (bgp, *as)
  918. && bgp->as != *as)
  919. local_as = bgp->confed_id;
  920. else
  921. local_as = bgp->as;
  922. /* If this is IPv4 unicast configuration and "no bgp default
  923. ipv4-unicast" is specified. */
  924. if (bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4)
  925. && afi == AFI_IP && safi == SAFI_UNICAST)
  926. peer_create (su, bgp, local_as, *as, 0, 0);
  927. else
  928. peer_create (su, bgp, local_as, *as, afi, safi);
  929. }
  930. return 0;
  931. }
  932. /* Activate the peer or peer group for specified AFI and SAFI. */
  933. int
  934. peer_activate (struct peer *peer, afi_t afi, safi_t safi)
  935. {
  936. int active;
  937. if (peer->afc[afi][safi])
  938. return 0;
  939. /* Activate the address family configuration. */
  940. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  941. peer->afc[afi][safi] = 1;
  942. else
  943. {
  944. active = peer_active (peer);
  945. peer->afc[afi][safi] = 1;
  946. if (! active && peer_active (peer))
  947. bgp_timer_set (peer);
  948. else
  949. {
  950. if (peer->status == Established)
  951. {
  952. if (CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV))
  953. {
  954. peer->afc_adv[afi][safi] = 1;
  955. bgp_capability_send (peer, afi, safi,
  956. CAPABILITY_CODE_MP,
  957. CAPABILITY_ACTION_SET);
  958. if (peer->afc_recv[afi][safi])
  959. {
  960. peer->afc_nego[afi][safi] = 1;
  961. bgp_announce_route (peer, afi, safi);
  962. }
  963. }
  964. else
  965. {
  966. peer->last_reset = PEER_DOWN_AF_ACTIVATE;
  967. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  968. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  969. }
  970. }
  971. }
  972. }
  973. return 0;
  974. }
  975. int
  976. peer_deactivate (struct peer *peer, afi_t afi, safi_t safi)
  977. {
  978. struct peer_group *group;
  979. struct peer *peer1;
  980. struct listnode *node, *nnode;
  981. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  982. {
  983. group = peer->group;
  984. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer1))
  985. {
  986. if (peer1->af_group[afi][safi])
  987. return BGP_ERR_PEER_GROUP_MEMBER_EXISTS;
  988. }
  989. }
  990. else
  991. {
  992. if (peer->af_group[afi][safi])
  993. return BGP_ERR_PEER_BELONGS_TO_GROUP;
  994. }
  995. if (! peer->afc[afi][safi])
  996. return 0;
  997. /* De-activate the address family configuration. */
  998. peer->afc[afi][safi] = 0;
  999. peer_af_flag_reset (peer, afi, safi);
  1000. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  1001. {
  1002. if (peer->status == Established)
  1003. {
  1004. if (CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV))
  1005. {
  1006. peer->afc_adv[afi][safi] = 0;
  1007. peer->afc_nego[afi][safi] = 0;
  1008. if (peer_active_nego (peer))
  1009. {
  1010. bgp_capability_send (peer, afi, safi,
  1011. CAPABILITY_CODE_MP,
  1012. CAPABILITY_ACTION_UNSET);
  1013. bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
  1014. peer->pcount[afi][safi] = 0;
  1015. }
  1016. else
  1017. {
  1018. peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
  1019. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  1020. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  1021. }
  1022. }
  1023. else
  1024. {
  1025. peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
  1026. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  1027. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  1028. }
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. int
  1034. peer_afc_set (struct peer *peer, afi_t afi, safi_t safi, int enable)
  1035. {
  1036. if (enable)
  1037. return peer_activate (peer, afi, safi);
  1038. else
  1039. return peer_deactivate (peer, afi, safi);
  1040. }
  1041. static void
  1042. peer_nsf_stop (struct peer *peer)
  1043. {
  1044. afi_t afi;
  1045. safi_t safi;
  1046. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  1047. UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
  1048. for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
  1049. for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
  1050. peer->nsf[afi][safi] = 0;
  1051. if (peer->t_gr_restart)
  1052. {
  1053. BGP_TIMER_OFF (peer->t_gr_restart);
  1054. if (BGP_DEBUG (events, EVENTS))
  1055. zlog_debug ("%s graceful restart timer stopped", peer->host);
  1056. }
  1057. if (peer->t_gr_stale)
  1058. {
  1059. BGP_TIMER_OFF (peer->t_gr_stale);
  1060. if (BGP_DEBUG (events, EVENTS))
  1061. zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
  1062. }
  1063. bgp_clear_route_all (peer);
  1064. }
  1065. /* Delete peer from confguration.
  1066. *
  1067. * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
  1068. * it to "cool off" and refcounts to hit 0, at which state it is freed.
  1069. *
  1070. * This function /should/ take care to be idempotent, to guard against
  1071. * it being called multiple times through stray events that come in
  1072. * that happen to result in this function being called again. That
  1073. * said, getting here for a "Deleted" peer is a bug in the neighbour
  1074. * FSM.
  1075. */
  1076. int
  1077. peer_delete (struct peer *peer)
  1078. {
  1079. int i;
  1080. afi_t afi;
  1081. safi_t safi;
  1082. struct bgp *bgp;
  1083. struct bgp_filter *filter;
  1084. struct listnode *pn;
  1085. assert (peer->status != Deleted);
  1086. bgp = peer->bgp;
  1087. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  1088. peer_nsf_stop (peer);
  1089. /* If this peer belongs to peer group, clear up the
  1090. relationship. */
  1091. if (peer->group)
  1092. {
  1093. if ((pn = listnode_lookup (peer->group->peer, peer)))
  1094. {
  1095. peer = peer_unlock (peer); /* group->peer list reference */
  1096. list_delete_node (peer->group->peer, pn);
  1097. }
  1098. peer->group = NULL;
  1099. }
  1100. /* Withdraw all information from routing table. We can not use
  1101. * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
  1102. * executed after peer structure is deleted.
  1103. */
  1104. peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
  1105. bgp_stop (peer);
  1106. bgp_fsm_change_status (peer, Deleted);
  1107. /* Remove from NHT */
  1108. bgp_unlink_nexthop_by_peer (peer);
  1109. /* Password configuration */
  1110. if (peer->password)
  1111. {
  1112. XFREE (MTYPE_PEER_PASSWORD, peer->password);
  1113. peer->password = NULL;
  1114. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  1115. bgp_md5_set (peer);
  1116. }
  1117. bgp_timer_set (peer); /* stops all timers for Deleted */
  1118. /* Delete from all peer list. */
  1119. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)
  1120. && (pn = listnode_lookup (bgp->peer, peer)))
  1121. {
  1122. peer_unlock (peer); /* bgp peer list reference */
  1123. list_delete_node (bgp->peer, pn);
  1124. }
  1125. if (peer_rsclient_active (peer)
  1126. && (pn = listnode_lookup (bgp->rsclient, peer)))
  1127. {
  1128. peer_unlock (peer); /* rsclient list reference */
  1129. list_delete_node (bgp->rsclient, pn);
  1130. /* Clear our own rsclient ribs. */
  1131. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  1132. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  1133. if (CHECK_FLAG(peer->af_flags[afi][safi],
  1134. PEER_FLAG_RSERVER_CLIENT))
  1135. bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_MY_RSCLIENT);
  1136. }
  1137. /* Free RIB for any family in which peer is RSERVER_CLIENT, and is not
  1138. member of a peer_group. */
  1139. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  1140. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  1141. if (peer->rib[afi][safi] && ! peer->af_group[afi][safi])
  1142. bgp_table_finish (&peer->rib[afi][safi]);
  1143. /* Buffers. */
  1144. if (peer->ibuf)
  1145. {
  1146. stream_free (peer->ibuf);
  1147. peer->ibuf = NULL;
  1148. }
  1149. if (peer->obuf)
  1150. {
  1151. stream_fifo_free (peer->obuf);
  1152. peer->obuf = NULL;
  1153. }
  1154. if (peer->work)
  1155. {
  1156. stream_free (peer->work);
  1157. peer->work = NULL;
  1158. }
  1159. if (peer->scratch)
  1160. {
  1161. stream_free(peer->scratch);
  1162. peer->scratch = NULL;
  1163. }
  1164. /* Local and remote addresses. */
  1165. if (peer->su_local)
  1166. {
  1167. sockunion_free (peer->su_local);
  1168. peer->su_local = NULL;
  1169. }
  1170. if (peer->su_remote)
  1171. {
  1172. sockunion_free (peer->su_remote);
  1173. peer->su_remote = NULL;
  1174. }
  1175. /* Free filter related memory. */
  1176. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  1177. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  1178. {
  1179. filter = &peer->filter[afi][safi];
  1180. for (i = FILTER_IN; i < FILTER_MAX; i++)
  1181. {
  1182. if (filter->dlist[i].name)
  1183. {
  1184. free(filter->dlist[i].name);
  1185. filter->dlist[i].name = NULL;
  1186. }
  1187. if (filter->plist[i].name)
  1188. {
  1189. free(filter->plist[i].name);
  1190. filter->plist[i].name = NULL;
  1191. }
  1192. if (filter->aslist[i].name)
  1193. {
  1194. free(filter->aslist[i].name);
  1195. filter->aslist[i].name = NULL;
  1196. }
  1197. }
  1198. for (i = RMAP_IN; i < RMAP_MAX; i++)
  1199. {
  1200. if (filter->map[i].name)
  1201. {
  1202. free (filter->map[i].name);
  1203. filter->map[i].name = NULL;
  1204. }
  1205. }
  1206. if (filter->usmap.name)
  1207. {
  1208. free (filter->usmap.name);
  1209. filter->usmap.name = NULL;
  1210. }
  1211. if (peer->default_rmap[afi][safi].name)
  1212. {
  1213. free (peer->default_rmap[afi][safi].name);
  1214. peer->default_rmap[afi][safi].name = NULL;
  1215. }
  1216. }
  1217. if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETING))
  1218. bgp_peer_clear_node_queue_drain_immediate(peer);
  1219. peer_unlock (peer); /* initial reference */
  1220. return 0;
  1221. }
  1222. static int
  1223. peer_group_cmp (struct peer_group *g1, struct peer_group *g2)
  1224. {
  1225. return strcmp (g1->name, g2->name);
  1226. }
  1227. /* If peer is configured at least one address family return 1. */
  1228. static int
  1229. peer_group_active (struct peer *peer)
  1230. {
  1231. if (peer->af_group[AFI_IP][SAFI_UNICAST]
  1232. || peer->af_group[AFI_IP][SAFI_MULTICAST]
  1233. || peer->af_group[AFI_IP][SAFI_MPLS_VPN]
  1234. || peer->af_group[AFI_IP][SAFI_ENCAP]
  1235. || peer->af_group[AFI_IP6][SAFI_UNICAST]
  1236. || peer->af_group[AFI_IP6][SAFI_MULTICAST]
  1237. || peer->af_group[AFI_IP6][SAFI_MPLS_VPN]
  1238. || peer->af_group[AFI_IP6][SAFI_ENCAP])
  1239. return 1;
  1240. return 0;
  1241. }
  1242. /* Peer group cofiguration. */
  1243. static struct peer_group *
  1244. peer_group_new (void)
  1245. {
  1246. return (struct peer_group *) XCALLOC (MTYPE_PEER_GROUP,
  1247. sizeof (struct peer_group));
  1248. }
  1249. static void
  1250. peer_group_free (struct peer_group *group)
  1251. {
  1252. XFREE (MTYPE_PEER_GROUP, group);
  1253. }
  1254. struct peer_group *
  1255. peer_group_lookup (struct bgp *bgp, const char *name)
  1256. {
  1257. struct peer_group *group;
  1258. struct listnode *node, *nnode;
  1259. for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group))
  1260. {
  1261. if (strcmp (group->name, name) == 0)
  1262. return group;
  1263. }
  1264. return NULL;
  1265. }
  1266. struct peer_group *
  1267. peer_group_get (struct bgp *bgp, const char *name)
  1268. {
  1269. struct peer_group *group;
  1270. group = peer_group_lookup (bgp, name);
  1271. if (group)
  1272. return group;
  1273. group = peer_group_new ();
  1274. group->bgp = bgp;
  1275. group->name = strdup (name);
  1276. group->peer = list_new ();
  1277. group->conf = peer_new (bgp);
  1278. if (! bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4))
  1279. group->conf->afc[AFI_IP][SAFI_UNICAST] = 1;
  1280. group->conf->host = XSTRDUP (MTYPE_BGP_PEER_HOST, name);
  1281. group->conf->group = group;
  1282. group->conf->as = 0;
  1283. group->conf->ttl = 0;
  1284. group->conf->gtsm_hops = 0;
  1285. group->conf->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  1286. UNSET_FLAG (group->conf->config, PEER_CONFIG_TIMER);
  1287. UNSET_FLAG (group->conf->config, PEER_CONFIG_CONNECT);
  1288. group->conf->keepalive = 0;
  1289. group->conf->holdtime = 0;
  1290. group->conf->connect = 0;
  1291. SET_FLAG (group->conf->sflags, PEER_STATUS_GROUP);
  1292. listnode_add_sort (bgp->group, group);
  1293. return 0;
  1294. }
  1295. static void
  1296. peer_group2peer_config_copy (struct peer_group *group, struct peer *peer,
  1297. afi_t afi, safi_t safi)
  1298. {
  1299. int in = FILTER_IN;
  1300. int out = FILTER_OUT;
  1301. struct peer *conf;
  1302. struct bgp_filter *pfilter;
  1303. struct bgp_filter *gfilter;
  1304. conf = group->conf;
  1305. pfilter = &peer->filter[afi][safi];
  1306. gfilter = &conf->filter[afi][safi];
  1307. /* remote-as */
  1308. if (conf->as)
  1309. peer->as = conf->as;
  1310. /* remote-as */
  1311. if (conf->change_local_as)
  1312. peer->change_local_as = conf->change_local_as;
  1313. /* TTL */
  1314. peer->ttl = conf->ttl;
  1315. /* GTSM hops */
  1316. peer->gtsm_hops = conf->gtsm_hops;
  1317. /* Weight */
  1318. peer->weight = conf->weight;
  1319. /* peer flags apply */
  1320. peer->flags = conf->flags;
  1321. /* peer af_flags apply */
  1322. peer->af_flags[afi][safi] = conf->af_flags[afi][safi];
  1323. /* peer config apply */
  1324. peer->config = conf->config;
  1325. /* peer timers apply */
  1326. peer->holdtime = conf->holdtime;
  1327. peer->keepalive = conf->keepalive;
  1328. peer->connect = conf->connect;
  1329. if (CHECK_FLAG (conf->config, PEER_CONFIG_CONNECT))
  1330. peer->v_connect = conf->connect;
  1331. else
  1332. peer->v_connect = BGP_DEFAULT_CONNECT_RETRY;
  1333. /* advertisement-interval reset */
  1334. if (CHECK_FLAG (conf->config, PEER_CONFIG_ROUTEADV))
  1335. peer->v_routeadv = conf->routeadv;
  1336. else
  1337. if (peer_sort (peer) == BGP_PEER_IBGP)
  1338. peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  1339. else
  1340. peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  1341. /* password apply */
  1342. if (conf->password && !peer->password)
  1343. peer->password = XSTRDUP (MTYPE_PEER_PASSWORD, conf->password);
  1344. bgp_md5_set (peer);
  1345. /* maximum-prefix */
  1346. peer->pmax[afi][safi] = conf->pmax[afi][safi];
  1347. peer->pmax_threshold[afi][safi] = conf->pmax_threshold[afi][safi];
  1348. peer->pmax_restart[afi][safi] = conf->pmax_restart[afi][safi];
  1349. /* allowas-in */
  1350. peer->allowas_in[afi][safi] = conf->allowas_in[afi][safi];
  1351. /* route-server-client */
  1352. if (CHECK_FLAG(conf->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  1353. {
  1354. /* Make peer's RIB point to group's RIB. */
  1355. peer->rib[afi][safi] = group->conf->rib[afi][safi];
  1356. /* Import policy. */
  1357. if (pfilter->map[RMAP_IMPORT].name)
  1358. free (pfilter->map[RMAP_IMPORT].name);
  1359. if (gfilter->map[RMAP_IMPORT].name)
  1360. {
  1361. pfilter->map[RMAP_IMPORT].name = strdup (gfilter->map[RMAP_IMPORT].name);
  1362. pfilter->map[RMAP_IMPORT].map = gfilter->map[RMAP_IMPORT].map;
  1363. }
  1364. else
  1365. {
  1366. pfilter->map[RMAP_IMPORT].name = NULL;
  1367. pfilter->map[RMAP_IMPORT].map = NULL;
  1368. }
  1369. /* Export policy. */
  1370. if (gfilter->map[RMAP_EXPORT].name && ! pfilter->map[RMAP_EXPORT].name)
  1371. {
  1372. pfilter->map[RMAP_EXPORT].name = strdup (gfilter->map[RMAP_EXPORT].name);
  1373. pfilter->map[RMAP_EXPORT].map = gfilter->map[RMAP_EXPORT].map;
  1374. }
  1375. }
  1376. /* default-originate route-map */
  1377. if (conf->default_rmap[afi][safi].name)
  1378. {
  1379. if (peer->default_rmap[afi][safi].name)
  1380. free (peer->default_rmap[afi][safi].name);
  1381. peer->default_rmap[afi][safi].name = strdup (conf->default_rmap[afi][safi].name);
  1382. peer->default_rmap[afi][safi].map = conf->default_rmap[afi][safi].map;
  1383. }
  1384. /* update-source apply */
  1385. if (conf->update_source)
  1386. {
  1387. if (peer->update_source)
  1388. sockunion_free (peer->update_source);
  1389. if (peer->update_if)
  1390. {
  1391. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  1392. peer->update_if = NULL;
  1393. }
  1394. peer->update_source = sockunion_dup (conf->update_source);
  1395. }
  1396. else if (conf->update_if)
  1397. {
  1398. if (peer->update_if)
  1399. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  1400. if (peer->update_source)
  1401. {
  1402. sockunion_free (peer->update_source);
  1403. peer->update_source = NULL;
  1404. }
  1405. peer->update_if = XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, conf->update_if);
  1406. }
  1407. /* inbound filter apply */
  1408. if (gfilter->dlist[in].name && ! pfilter->dlist[in].name)
  1409. {
  1410. if (pfilter->dlist[in].name)
  1411. free (pfilter->dlist[in].name);
  1412. pfilter->dlist[in].name = strdup (gfilter->dlist[in].name);
  1413. pfilter->dlist[in].alist = gfilter->dlist[in].alist;
  1414. }
  1415. if (gfilter->plist[in].name && ! pfilter->plist[in].name)
  1416. {
  1417. if (pfilter->plist[in].name)
  1418. free (pfilter->plist[in].name);
  1419. pfilter->plist[in].name = strdup (gfilter->plist[in].name);
  1420. pfilter->plist[in].plist = gfilter->plist[in].plist;
  1421. }
  1422. if (gfilter->aslist[in].name && ! pfilter->aslist[in].name)
  1423. {
  1424. if (pfilter->aslist[in].name)
  1425. free (pfilter->aslist[in].name);
  1426. pfilter->aslist[in].name = strdup (gfilter->aslist[in].name);
  1427. pfilter->aslist[in].aslist = gfilter->aslist[in].aslist;
  1428. }
  1429. if (gfilter->map[RMAP_IN].name && ! pfilter->map[RMAP_IN].name)
  1430. {
  1431. if (pfilter->map[RMAP_IN].name)
  1432. free (pfilter->map[RMAP_IN].name);
  1433. pfilter->map[RMAP_IN].name = strdup (gfilter->map[RMAP_IN].name);
  1434. pfilter->map[RMAP_IN].map = gfilter->map[RMAP_IN].map;
  1435. }
  1436. /* outbound filter apply */
  1437. if (gfilter->dlist[out].name)
  1438. {
  1439. if (pfilter->dlist[out].name)
  1440. free (pfilter->dlist[out].name);
  1441. pfilter->dlist[out].name = strdup (gfilter->dlist[out].name);
  1442. pfilter->dlist[out].alist = gfilter->dlist[out].alist;
  1443. }
  1444. else
  1445. {
  1446. if (pfilter->dlist[out].name)
  1447. free (pfilter->dlist[out].name);
  1448. pfilter->dlist[out].name = NULL;
  1449. pfilter->dlist[out].alist = NULL;
  1450. }
  1451. if (gfilter->plist[out].name)
  1452. {
  1453. if (pfilter->plist[out].name)
  1454. free (pfilter->plist[out].name);
  1455. pfilter->plist[out].name = strdup (gfilter->plist[out].name);
  1456. pfilter->plist[out].plist = gfilter->plist[out].plist;
  1457. }
  1458. else
  1459. {
  1460. if (pfilter->plist[out].name)
  1461. free (pfilter->plist[out].name);
  1462. pfilter->plist[out].name = NULL;
  1463. pfilter->plist[out].plist = NULL;
  1464. }
  1465. if (gfilter->aslist[out].name)
  1466. {
  1467. if (pfilter->aslist[out].name)
  1468. free (pfilter->aslist[out].name);
  1469. pfilter->aslist[out].name = strdup (gfilter->aslist[out].name);
  1470. pfilter->aslist[out].aslist = gfilter->aslist[out].aslist;
  1471. }
  1472. else
  1473. {
  1474. if (pfilter->aslist[out].name)
  1475. free (pfilter->aslist[out].name);
  1476. pfilter->aslist[out].name = NULL;
  1477. pfilter->aslist[out].aslist = NULL;
  1478. }
  1479. if (gfilter->map[RMAP_OUT].name)
  1480. {
  1481. if (pfilter->map[RMAP_OUT].name)
  1482. free (pfilter->map[RMAP_OUT].name);
  1483. pfilter->map[RMAP_OUT].name = strdup (gfilter->map[RMAP_OUT].name);
  1484. pfilter->map[RMAP_OUT].map = gfilter->map[RMAP_OUT].map;
  1485. }
  1486. else
  1487. {
  1488. if (pfilter->map[RMAP_OUT].name)
  1489. free (pfilter->map[RMAP_OUT].name);
  1490. pfilter->map[RMAP_OUT].name = NULL;
  1491. pfilter->map[RMAP_OUT].map = NULL;
  1492. }
  1493. /* RS-client's import/export route-maps. */
  1494. if (gfilter->map[RMAP_IMPORT].name)
  1495. {
  1496. if (pfilter->map[RMAP_IMPORT].name)
  1497. free (pfilter->map[RMAP_IMPORT].name);
  1498. pfilter->map[RMAP_IMPORT].name = strdup (gfilter->map[RMAP_IMPORT].name);
  1499. pfilter->map[RMAP_IMPORT].map = gfilter->map[RMAP_IMPORT].map;
  1500. }
  1501. else
  1502. {
  1503. if (pfilter->map[RMAP_IMPORT].name)
  1504. free (pfilter->map[RMAP_IMPORT].name);
  1505. pfilter->map[RMAP_IMPORT].name = NULL;
  1506. pfilter->map[RMAP_IMPORT].map = NULL;
  1507. }
  1508. if (gfilter->map[RMAP_EXPORT].name && ! pfilter->map[RMAP_EXPORT].name)
  1509. {
  1510. if (pfilter->map[RMAP_EXPORT].name)
  1511. free (pfilter->map[RMAP_EXPORT].name);
  1512. pfilter->map[RMAP_EXPORT].name = strdup (gfilter->map[RMAP_EXPORT].name);
  1513. pfilter->map[RMAP_EXPORT].map = gfilter->map[RMAP_EXPORT].map;
  1514. }
  1515. if (gfilter->usmap.name)
  1516. {
  1517. if (pfilter->usmap.name)
  1518. free (pfilter->usmap.name);
  1519. pfilter->usmap.name = strdup (gfilter->usmap.name);
  1520. pfilter->usmap.map = gfilter->usmap.map;
  1521. }
  1522. else
  1523. {
  1524. if (pfilter->usmap.name)
  1525. free (pfilter->usmap.name);
  1526. pfilter->usmap.name = NULL;
  1527. pfilter->usmap.map = NULL;
  1528. }
  1529. }
  1530. /* Peer group's remote AS configuration. */
  1531. int
  1532. peer_group_remote_as (struct bgp *bgp, const char *group_name, as_t *as)
  1533. {
  1534. struct peer_group *group;
  1535. struct peer *peer;
  1536. struct listnode *node, *nnode;
  1537. group = peer_group_lookup (bgp, group_name);
  1538. if (! group)
  1539. return -1;
  1540. if (group->conf->as == *as)
  1541. return 0;
  1542. /* When we setup peer-group AS number all peer group member's AS
  1543. number must be updated to same number. */
  1544. peer_as_change (group->conf, *as);
  1545. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  1546. {
  1547. if (peer->as != *as)
  1548. peer_as_change (peer, *as);
  1549. }
  1550. return 0;
  1551. }
  1552. int
  1553. peer_ttl (struct peer *peer)
  1554. {
  1555. if (peer->ttl)
  1556. return peer->ttl;
  1557. if (peer->gtsm_hops || peer->sort == BGP_PEER_IBGP)
  1558. return 255;
  1559. return 1;
  1560. }
  1561. int
  1562. peer_group_delete (struct peer_group *group)
  1563. {
  1564. struct bgp *bgp;
  1565. struct peer *peer;
  1566. struct listnode *node, *nnode;
  1567. bgp = group->bgp;
  1568. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  1569. {
  1570. peer_delete (peer);
  1571. }
  1572. list_delete (group->peer);
  1573. free (group->name);
  1574. group->name = NULL;
  1575. group->conf->group = NULL;
  1576. peer_delete (group->conf);
  1577. /* Delete from all peer_group list. */
  1578. listnode_delete (bgp->group, group);
  1579. peer_group_free (group);
  1580. return 0;
  1581. }
  1582. int
  1583. peer_group_remote_as_delete (struct peer_group *group)
  1584. {
  1585. struct peer *peer;
  1586. struct listnode *node, *nnode;
  1587. if (! group->conf->as)
  1588. return 0;
  1589. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  1590. {
  1591. peer_delete (peer);
  1592. }
  1593. list_delete_all_node (group->peer);
  1594. group->conf->as = 0;
  1595. return 0;
  1596. }
  1597. /* Bind specified peer to peer group. */
  1598. int
  1599. peer_group_bind (struct bgp *bgp, union sockunion *su,
  1600. struct peer_group *group, afi_t afi, safi_t safi, as_t *as)
  1601. {
  1602. struct peer *peer;
  1603. int first_member = 0;
  1604. /* Check peer group's address family. */
  1605. if (! group->conf->afc[afi][safi])
  1606. return BGP_ERR_PEER_GROUP_AF_UNCONFIGURED;
  1607. /* Lookup the peer. */
  1608. peer = peer_lookup (bgp, su);
  1609. /* Create a new peer. */
  1610. if (! peer)
  1611. {
  1612. if (! group->conf->as)
  1613. return BGP_ERR_PEER_GROUP_NO_REMOTE_AS;
  1614. peer = peer_create (su, bgp, bgp->as, group->conf->as, afi, safi);
  1615. peer->group = group;
  1616. peer->af_group[afi][safi] = 1;
  1617. peer = peer_lock (peer); /* group->peer list reference */
  1618. listnode_add (group->peer, peer);
  1619. peer_group2peer_config_copy (group, peer, afi, safi);
  1620. return 0;
  1621. }
  1622. /* When the peer already belongs to peer group, check the consistency. */
  1623. if (peer->af_group[afi][safi])
  1624. {
  1625. if (strcmp (peer->group->name, group->name) != 0)
  1626. return BGP_ERR_PEER_GROUP_CANT_CHANGE;
  1627. return 0;
  1628. }
  1629. /* Check current peer group configuration. */
  1630. if (peer_group_active (peer)
  1631. && strcmp (peer->group->name, group->name) != 0)
  1632. return BGP_ERR_PEER_GROUP_MISMATCH;
  1633. if (! group->conf->as)
  1634. {
  1635. if (peer_sort (group->conf) != BGP_PEER_INTERNAL
  1636. && peer_sort (group->conf) != peer_sort (peer))
  1637. {
  1638. if (as)
  1639. *as = peer->as;
  1640. return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
  1641. }
  1642. if (peer_sort (group->conf) == BGP_PEER_INTERNAL)
  1643. first_member = 1;
  1644. }
  1645. peer->af_group[afi][safi] = 1;
  1646. peer->afc[afi][safi] = 1;
  1647. if (! peer->group)
  1648. {
  1649. peer->group = group;
  1650. peer = peer_lock (peer); /* group->peer list reference */
  1651. listnode_add (group->peer, peer);
  1652. }
  1653. else
  1654. assert (group && peer->group == group);
  1655. if (first_member)
  1656. {
  1657. /* Advertisement-interval reset */
  1658. if (! CHECK_FLAG (group->conf->config, PEER_CONFIG_ROUTEADV))
  1659. {
  1660. if (peer_sort (group->conf) == BGP_PEER_IBGP)
  1661. group->conf->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  1662. else
  1663. group->conf->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  1664. }
  1665. /* local-as reset */
  1666. if (peer_sort (group->conf) != BGP_PEER_EBGP)
  1667. {
  1668. group->conf->change_local_as = 0;
  1669. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  1670. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  1671. }
  1672. }
  1673. if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  1674. {
  1675. struct listnode *pn;
  1676. /* If it's not configured as RSERVER_CLIENT in any other address
  1677. family, without being member of a peer_group, remove it from
  1678. list bgp->rsclient.*/
  1679. if (! peer_rsclient_active (peer)
  1680. && (pn = listnode_lookup (bgp->rsclient, peer)))
  1681. {
  1682. peer_unlock (peer); /* peer rsclient reference */
  1683. list_delete_node (bgp->rsclient, pn);
  1684. /* Clear our own rsclient rib for this afi/safi. */
  1685. bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_MY_RSCLIENT);
  1686. }
  1687. bgp_table_finish (&peer->rib[afi][safi]);
  1688. /* Import policy. */
  1689. if (peer->filter[afi][safi].map[RMAP_IMPORT].name)
  1690. {
  1691. free (peer->filter[afi][safi].map[RMAP_IMPORT].name);
  1692. peer->filter[afi][safi].map[RMAP_IMPORT].name = NULL;
  1693. peer->filter[afi][safi].map[RMAP_IMPORT].map = NULL;
  1694. }
  1695. /* Export policy. */
  1696. if (! CHECK_FLAG(group->conf->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
  1697. && peer->filter[afi][safi].map[RMAP_EXPORT].name)
  1698. {
  1699. free (peer->filter[afi][safi].map[RMAP_EXPORT].name);
  1700. peer->filter[afi][safi].map[RMAP_EXPORT].name = NULL;
  1701. peer->filter[afi][safi].map[RMAP_EXPORT].map = NULL;
  1702. }
  1703. }
  1704. peer_group2peer_config_copy (group, peer, afi, safi);
  1705. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  1706. {
  1707. peer->last_reset = PEER_DOWN_RMAP_BIND;
  1708. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  1709. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  1710. }
  1711. else
  1712. BGP_EVENT_ADD (peer, BGP_Stop);
  1713. return 0;
  1714. }
  1715. int
  1716. peer_group_unbind (struct bgp *bgp, struct peer *peer,
  1717. struct peer_group *group, afi_t afi, safi_t safi)
  1718. {
  1719. if (! peer->af_group[afi][safi])
  1720. return 0;
  1721. if (group != peer->group)
  1722. return BGP_ERR_PEER_GROUP_MISMATCH;
  1723. peer->af_group[afi][safi] = 0;
  1724. peer->afc[afi][safi] = 0;
  1725. peer_af_flag_reset (peer, afi, safi);
  1726. if (peer->rib[afi][safi])
  1727. peer->rib[afi][safi] = NULL;
  1728. if (! peer_group_active (peer))
  1729. {
  1730. assert (listnode_lookup (group->peer, peer));
  1731. peer_unlock (peer); /* peer group list reference */
  1732. listnode_delete (group->peer, peer);
  1733. peer->group = NULL;
  1734. if (group->conf->as)
  1735. {
  1736. peer_delete (peer);
  1737. return 0;
  1738. }
  1739. peer_global_config_reset (peer);
  1740. }
  1741. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  1742. {
  1743. peer->last_reset = PEER_DOWN_RMAP_UNBIND;
  1744. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  1745. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  1746. }
  1747. else
  1748. BGP_EVENT_ADD (peer, BGP_Stop);
  1749. return 0;
  1750. }
  1751. static int
  1752. bgp_startup_timer_expire (struct thread *thread)
  1753. {
  1754. struct bgp *bgp;
  1755. bgp = THREAD_ARG (thread);
  1756. bgp->t_startup = NULL;
  1757. return 0;
  1758. }
  1759. /* BGP instance creation by `router bgp' commands. */
  1760. static struct bgp *
  1761. bgp_create (as_t *as, const char *name)
  1762. {
  1763. struct bgp *bgp;
  1764. afi_t afi;
  1765. safi_t safi;
  1766. if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL)
  1767. return NULL;
  1768. bgp_lock (bgp);
  1769. bgp->peer_self = peer_new (bgp);
  1770. bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement");
  1771. bgp->peer = list_new ();
  1772. bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
  1773. bgp->group = list_new ();
  1774. bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
  1775. bgp->rsclient = list_new ();
  1776. bgp->rsclient->cmp = (int (*)(void*, void*)) peer_cmp;
  1777. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  1778. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  1779. {
  1780. bgp->route[afi][safi] = bgp_table_init (afi, safi);
  1781. bgp->aggregate[afi][safi] = bgp_table_init (afi, safi);
  1782. bgp->rib[afi][safi] = bgp_table_init (afi, safi);
  1783. bgp->maxpaths[afi][safi].maxpaths_ebgp = BGP_DEFAULT_MAXPATHS;
  1784. bgp->maxpaths[afi][safi].maxpaths_ibgp = BGP_DEFAULT_MAXPATHS;
  1785. }
  1786. bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
  1787. bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
  1788. bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
  1789. bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
  1790. bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
  1791. bgp_flag_set (bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES);
  1792. bgp->as = *as;
  1793. if (name)
  1794. bgp->name = strdup (name);
  1795. THREAD_TIMER_ON (bm->master, bgp->t_startup, bgp_startup_timer_expire,
  1796. bgp, bgp->restart_time);
  1797. return bgp;
  1798. }
  1799. /* Return first entry of BGP. */
  1800. struct bgp *
  1801. bgp_get_default (void)
  1802. {
  1803. if (bm && bm->bgp && bm->bgp->head)
  1804. return (listgetdata (listhead (bm->bgp)));
  1805. return NULL;
  1806. }
  1807. /* Lookup BGP entry. */
  1808. struct bgp *
  1809. bgp_lookup (as_t as, const char *name)
  1810. {
  1811. struct bgp *bgp;
  1812. struct listnode *node, *nnode;
  1813. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  1814. if (bgp->as == as
  1815. && ((bgp->name == NULL && name == NULL)
  1816. || (bgp->name && name && strcmp (bgp->name, name) == 0)))
  1817. return bgp;
  1818. return NULL;
  1819. }
  1820. /* Lookup BGP structure by view name. */
  1821. struct bgp *
  1822. bgp_lookup_by_name (const char *name)
  1823. {
  1824. struct bgp *bgp;
  1825. struct listnode *node, *nnode;
  1826. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  1827. if ((bgp->name == NULL && name == NULL)
  1828. || (bgp->name && name && strcmp (bgp->name, name) == 0))
  1829. return bgp;
  1830. return NULL;
  1831. }
  1832. /* Called from VTY commands. */
  1833. int
  1834. bgp_get (struct bgp **bgp_val, as_t *as, const char *name)
  1835. {
  1836. struct bgp *bgp;
  1837. /* Multiple instance check. */
  1838. if (bgp_option_check (BGP_OPT_MULTIPLE_INSTANCE))
  1839. {
  1840. if (name)
  1841. bgp = bgp_lookup_by_name (name);
  1842. else
  1843. bgp = bgp_get_default ();
  1844. /* Already exists. */
  1845. if (bgp)
  1846. {
  1847. if (bgp->as != *as)
  1848. {
  1849. *as = bgp->as;
  1850. return BGP_ERR_INSTANCE_MISMATCH;
  1851. }
  1852. *bgp_val = bgp;
  1853. return 0;
  1854. }
  1855. }
  1856. else
  1857. {
  1858. /* BGP instance name can not be specified for single instance. */
  1859. if (name)
  1860. return BGP_ERR_MULTIPLE_INSTANCE_NOT_SET;
  1861. /* Get default BGP structure if exists. */
  1862. bgp = bgp_get_default ();
  1863. if (bgp)
  1864. {
  1865. if (bgp->as != *as)
  1866. {
  1867. *as = bgp->as;
  1868. return BGP_ERR_AS_MISMATCH;
  1869. }
  1870. *bgp_val = bgp;
  1871. return 0;
  1872. }
  1873. }
  1874. bgp = bgp_create (as, name);
  1875. bgp_router_id_set(bgp, &router_id_zebra);
  1876. *bgp_val = bgp;
  1877. /* Create BGP server socket, if first instance. */
  1878. if (list_isempty(bm->bgp)
  1879. && !bgp_option_check (BGP_OPT_NO_LISTEN))
  1880. {
  1881. if (bgp_socket (bm->port, bm->address) < 0)
  1882. return BGP_ERR_INVALID_VALUE;
  1883. }
  1884. listnode_add (bm->bgp, bgp);
  1885. return 0;
  1886. }
  1887. /* Delete BGP instance. */
  1888. int
  1889. bgp_delete (struct bgp *bgp)
  1890. {
  1891. struct peer *peer;
  1892. struct peer_group *group;
  1893. struct listnode *node, *pnode;
  1894. struct listnode *next, *pnext;
  1895. afi_t afi;
  1896. int i;
  1897. SET_FLAG(bgp->flags, BGP_FLAG_DELETING);
  1898. THREAD_OFF (bgp->t_startup);
  1899. for (ALL_LIST_ELEMENTS (bgp->peer, node, next, peer))
  1900. {
  1901. if (peer->status == Established ||
  1902. peer->status == OpenSent ||
  1903. peer->status == OpenConfirm)
  1904. {
  1905. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  1906. BGP_NOTIFY_CEASE_PEER_UNCONFIG);
  1907. }
  1908. }
  1909. /* Delete static route. */
  1910. bgp_static_delete (bgp);
  1911. /* Unset redistribution. */
  1912. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  1913. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  1914. if (i != ZEBRA_ROUTE_BGP)
  1915. bgp_redistribute_unset (bgp, afi, i);
  1916. for (ALL_LIST_ELEMENTS (bgp->peer, node, next, peer))
  1917. {
  1918. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  1919. {
  1920. /* Send notify to remote peer. */
  1921. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
  1922. }
  1923. peer_delete (peer);
  1924. }
  1925. for (ALL_LIST_ELEMENTS (bgp->group, node, next, group))
  1926. {
  1927. for (ALL_LIST_ELEMENTS (group->peer, pnode, pnext, peer))
  1928. {
  1929. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  1930. {
  1931. /* Send notify to remote peer. */
  1932. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
  1933. }
  1934. }
  1935. peer_group_delete (group);
  1936. }
  1937. assert (listcount (bgp->rsclient) == 0);
  1938. if (bgp->peer_self) {
  1939. peer_delete(bgp->peer_self);
  1940. bgp->peer_self = NULL;
  1941. }
  1942. /*
  1943. * Free pending deleted routes. Unfortunately, it also has to process
  1944. * all the pending activity for other instances of struct bgp.
  1945. *
  1946. * This call was added to achieve clean memory allocation at exit,
  1947. * for the sake of valgrind.
  1948. */
  1949. bgp_process_queues_drain_immediate();
  1950. /* Remove visibility via the master list - there may however still be
  1951. * routes to be processed still referencing the struct bgp.
  1952. */
  1953. listnode_delete (bm->bgp, bgp);
  1954. if (list_isempty(bm->bgp))
  1955. bgp_close ();
  1956. bgp_unlock(bgp); /* initial reference */
  1957. return 0;
  1958. }
  1959. static void bgp_free (struct bgp *);
  1960. void
  1961. bgp_lock (struct bgp *bgp)
  1962. {
  1963. ++bgp->lock;
  1964. }
  1965. void
  1966. bgp_unlock(struct bgp *bgp)
  1967. {
  1968. assert(bgp->lock > 0);
  1969. if (--bgp->lock == 0)
  1970. bgp_free (bgp);
  1971. }
  1972. static void
  1973. bgp_free (struct bgp *bgp)
  1974. {
  1975. afi_t afi;
  1976. safi_t safi;
  1977. list_delete (bgp->group);
  1978. list_delete (bgp->peer);
  1979. list_delete (bgp->rsclient);
  1980. if (bgp->name)
  1981. free (bgp->name);
  1982. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  1983. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  1984. {
  1985. if (bgp->route[afi][safi])
  1986. bgp_table_finish (&bgp->route[afi][safi]);
  1987. if (bgp->aggregate[afi][safi])
  1988. bgp_table_finish (&bgp->aggregate[afi][safi]) ;
  1989. if (bgp->rib[afi][safi])
  1990. bgp_table_finish (&bgp->rib[afi][safi]);
  1991. }
  1992. XFREE (MTYPE_BGP, bgp);
  1993. }
  1994. struct peer *
  1995. peer_lookup (struct bgp *bgp, union sockunion *su)
  1996. {
  1997. struct peer *peer;
  1998. struct listnode *node, *nnode;
  1999. if (bgp != NULL)
  2000. {
  2001. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  2002. if (sockunion_same (&peer->su, su)
  2003. && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  2004. return peer;
  2005. }
  2006. else if (bm->bgp != NULL)
  2007. {
  2008. struct listnode *bgpnode, *nbgpnode;
  2009. for (ALL_LIST_ELEMENTS (bm->bgp, bgpnode, nbgpnode, bgp))
  2010. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  2011. if (sockunion_same (&peer->su, su)
  2012. && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  2013. return peer;
  2014. }
  2015. return NULL;
  2016. }
  2017. struct peer *
  2018. peer_lookup_with_open (union sockunion *su, as_t remote_as,
  2019. struct in_addr *remote_id, int *as)
  2020. {
  2021. struct peer *peer;
  2022. struct listnode *node;
  2023. struct listnode *bgpnode;
  2024. struct bgp *bgp;
  2025. if (! bm->bgp)
  2026. return NULL;
  2027. for (ALL_LIST_ELEMENTS_RO (bm->bgp, bgpnode, bgp))
  2028. {
  2029. for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
  2030. {
  2031. if (sockunion_same (&peer->su, su)
  2032. && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  2033. {
  2034. if (peer->as == remote_as
  2035. && peer->remote_id.s_addr == remote_id->s_addr)
  2036. return peer;
  2037. if (peer->as == remote_as)
  2038. *as = 1;
  2039. }
  2040. }
  2041. for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
  2042. {
  2043. if (sockunion_same (&peer->su, su)
  2044. && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  2045. {
  2046. if (peer->as == remote_as
  2047. && peer->remote_id.s_addr == 0)
  2048. return peer;
  2049. if (peer->as == remote_as)
  2050. *as = 1;
  2051. }
  2052. }
  2053. }
  2054. return NULL;
  2055. }
  2056. /* If peer is configured at least one address family return 1. */
  2057. int
  2058. peer_active (struct peer *peer)
  2059. {
  2060. if (peer->afc[AFI_IP][SAFI_UNICAST]
  2061. || peer->afc[AFI_IP][SAFI_MULTICAST]
  2062. || peer->afc[AFI_IP][SAFI_MPLS_VPN]
  2063. || peer->afc[AFI_IP][SAFI_ENCAP]
  2064. || peer->afc[AFI_IP6][SAFI_UNICAST]
  2065. || peer->afc[AFI_IP6][SAFI_MULTICAST]
  2066. || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
  2067. || peer->afc[AFI_IP6][SAFI_ENCAP])
  2068. return 1;
  2069. return 0;
  2070. }
  2071. /* If peer is negotiated at least one address family return 1. */
  2072. int
  2073. peer_active_nego (struct peer *peer)
  2074. {
  2075. if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
  2076. || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
  2077. || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
  2078. || peer->afc_nego[AFI_IP][SAFI_ENCAP]
  2079. || peer->afc_nego[AFI_IP6][SAFI_UNICAST]
  2080. || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
  2081. || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
  2082. || peer->afc_nego[AFI_IP6][SAFI_ENCAP])
  2083. return 1;
  2084. return 0;
  2085. }
  2086. /* peer_flag_change_type. */
  2087. enum peer_change_type
  2088. {
  2089. peer_change_none,
  2090. peer_change_reset,
  2091. peer_change_reset_in,
  2092. peer_change_reset_out,
  2093. };
  2094. static void
  2095. peer_change_action (struct peer *peer, afi_t afi, safi_t safi,
  2096. enum peer_change_type type)
  2097. {
  2098. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2099. return;
  2100. if (peer->status != Established)
  2101. return;
  2102. if (type == peer_change_reset)
  2103. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2104. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2105. else if (type == peer_change_reset_in)
  2106. {
  2107. if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV)
  2108. || CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
  2109. bgp_route_refresh_send (peer, afi, safi, 0, 0, 0);
  2110. else
  2111. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2112. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2113. }
  2114. else if (type == peer_change_reset_out)
  2115. bgp_announce_route (peer, afi, safi);
  2116. }
  2117. struct peer_flag_action
  2118. {
  2119. /* Peer's flag. */
  2120. u_int32_t flag;
  2121. /* This flag can be set for peer-group member. */
  2122. u_char not_for_member;
  2123. /* Action when the flag is changed. */
  2124. enum peer_change_type type;
  2125. /* Peer down cause */
  2126. u_char peer_down;
  2127. };
  2128. static const struct peer_flag_action peer_flag_action_list[] =
  2129. {
  2130. { PEER_FLAG_PASSIVE, 0, peer_change_reset },
  2131. { PEER_FLAG_SHUTDOWN, 0, peer_change_reset },
  2132. { PEER_FLAG_DONT_CAPABILITY, 0, peer_change_none },
  2133. { PEER_FLAG_OVERRIDE_CAPABILITY, 0, peer_change_none },
  2134. { PEER_FLAG_STRICT_CAP_MATCH, 0, peer_change_none },
  2135. { PEER_FLAG_DYNAMIC_CAPABILITY, 0, peer_change_reset },
  2136. { PEER_FLAG_DISABLE_CONNECTED_CHECK, 0, peer_change_reset },
  2137. { 0, 0, 0 }
  2138. };
  2139. static const struct peer_flag_action peer_af_flag_action_list[] =
  2140. {
  2141. { PEER_FLAG_NEXTHOP_SELF, 1, peer_change_reset_out },
  2142. { PEER_FLAG_SEND_COMMUNITY, 1, peer_change_reset_out },
  2143. { PEER_FLAG_SEND_EXT_COMMUNITY, 1, peer_change_reset_out },
  2144. { PEER_FLAG_SEND_LARGE_COMMUNITY, 1, peer_change_reset_out },
  2145. { PEER_FLAG_SOFT_RECONFIG, 0, peer_change_reset_in },
  2146. { PEER_FLAG_REFLECTOR_CLIENT, 1, peer_change_reset },
  2147. { PEER_FLAG_RSERVER_CLIENT, 1, peer_change_reset },
  2148. { PEER_FLAG_AS_PATH_UNCHANGED, 1, peer_change_reset_out },
  2149. { PEER_FLAG_NEXTHOP_UNCHANGED, 1, peer_change_reset_out },
  2150. { PEER_FLAG_MED_UNCHANGED, 1, peer_change_reset_out },
  2151. { PEER_FLAG_REMOVE_PRIVATE_AS, 1, peer_change_reset_out },
  2152. { PEER_FLAG_ALLOWAS_IN, 0, peer_change_reset_in },
  2153. { PEER_FLAG_ORF_PREFIX_SM, 1, peer_change_reset },
  2154. { PEER_FLAG_ORF_PREFIX_RM, 1, peer_change_reset },
  2155. { PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED, 0, peer_change_reset_out },
  2156. { PEER_FLAG_NEXTHOP_SELF_ALL, 1, peer_change_reset_out },
  2157. { 0, 0, 0 }
  2158. };
  2159. /* Proper action set. */
  2160. static int
  2161. peer_flag_action_set (const struct peer_flag_action *action_list, int size,
  2162. struct peer_flag_action *action, u_int32_t flag)
  2163. {
  2164. int i;
  2165. int found = 0;
  2166. int reset_in = 0;
  2167. int reset_out = 0;
  2168. const struct peer_flag_action *match = NULL;
  2169. /* Check peer's frag action. */
  2170. for (i = 0; i < size; i++)
  2171. {
  2172. match = &action_list[i];
  2173. if (match->flag == 0)
  2174. break;
  2175. if (match->flag & flag)
  2176. {
  2177. found = 1;
  2178. if (match->type == peer_change_reset_in)
  2179. reset_in = 1;
  2180. if (match->type == peer_change_reset_out)
  2181. reset_out = 1;
  2182. if (match->type == peer_change_reset)
  2183. {
  2184. reset_in = 1;
  2185. reset_out = 1;
  2186. }
  2187. if (match->not_for_member)
  2188. action->not_for_member = 1;
  2189. }
  2190. }
  2191. /* Set peer clear type. */
  2192. if (reset_in && reset_out)
  2193. action->type = peer_change_reset;
  2194. else if (reset_in)
  2195. action->type = peer_change_reset_in;
  2196. else if (reset_out)
  2197. action->type = peer_change_reset_out;
  2198. else
  2199. action->type = peer_change_none;
  2200. return found;
  2201. }
  2202. static void
  2203. peer_flag_modify_action (struct peer *peer, u_int32_t flag)
  2204. {
  2205. if (flag == PEER_FLAG_SHUTDOWN)
  2206. {
  2207. if (CHECK_FLAG (peer->flags, flag))
  2208. {
  2209. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  2210. peer_nsf_stop (peer);
  2211. UNSET_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
  2212. if (peer->t_pmax_restart)
  2213. {
  2214. BGP_TIMER_OFF (peer->t_pmax_restart);
  2215. if (BGP_DEBUG (events, EVENTS))
  2216. zlog_debug ("%s Maximum-prefix restart timer canceled",
  2217. peer->host);
  2218. }
  2219. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
  2220. peer_nsf_stop (peer);
  2221. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2222. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2223. BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
  2224. else
  2225. BGP_EVENT_ADD (peer, BGP_Stop);
  2226. }
  2227. else
  2228. {
  2229. peer->v_start = BGP_INIT_START_TIMER;
  2230. BGP_EVENT_ADD (peer, BGP_Stop);
  2231. }
  2232. }
  2233. else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2234. {
  2235. if (flag == PEER_FLAG_DYNAMIC_CAPABILITY)
  2236. peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
  2237. else if (flag == PEER_FLAG_PASSIVE)
  2238. peer->last_reset = PEER_DOWN_PASSIVE_CHANGE;
  2239. else if (flag == PEER_FLAG_DISABLE_CONNECTED_CHECK)
  2240. peer->last_reset = PEER_DOWN_MULTIHOP_CHANGE;
  2241. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2242. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2243. }
  2244. else
  2245. BGP_EVENT_ADD (peer, BGP_Stop);
  2246. }
  2247. /* Change specified peer flag. */
  2248. static int
  2249. peer_flag_modify (struct peer *peer, u_int32_t flag, int set)
  2250. {
  2251. int found;
  2252. int size;
  2253. struct peer_group *group;
  2254. struct listnode *node, *nnode;
  2255. struct peer_flag_action action;
  2256. memset (&action, 0, sizeof (struct peer_flag_action));
  2257. size = sizeof peer_flag_action_list / sizeof (struct peer_flag_action);
  2258. found = peer_flag_action_set (peer_flag_action_list, size, &action, flag);
  2259. /* No flag action is found. */
  2260. if (! found)
  2261. return BGP_ERR_INVALID_FLAG;
  2262. /* Not for peer-group member. */
  2263. if (action.not_for_member && peer_group_active (peer))
  2264. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2265. /* When unset the peer-group member's flag we have to check
  2266. peer-group configuration. */
  2267. if (! set && peer_group_active (peer))
  2268. if (CHECK_FLAG (peer->group->conf->flags, flag))
  2269. {
  2270. if (flag == PEER_FLAG_SHUTDOWN)
  2271. return BGP_ERR_PEER_GROUP_SHUTDOWN;
  2272. else
  2273. return BGP_ERR_PEER_GROUP_HAS_THE_FLAG;
  2274. }
  2275. /* Flag conflict check. */
  2276. if (set
  2277. && CHECK_FLAG (peer->flags | flag, PEER_FLAG_STRICT_CAP_MATCH)
  2278. && CHECK_FLAG (peer->flags | flag, PEER_FLAG_OVERRIDE_CAPABILITY))
  2279. return BGP_ERR_PEER_FLAG_CONFLICT;
  2280. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2281. {
  2282. if (set && CHECK_FLAG (peer->flags, flag) == flag)
  2283. return 0;
  2284. if (! set && ! CHECK_FLAG (peer->flags, flag))
  2285. return 0;
  2286. }
  2287. if (set)
  2288. SET_FLAG (peer->flags, flag);
  2289. else
  2290. UNSET_FLAG (peer->flags, flag);
  2291. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2292. {
  2293. if (action.type == peer_change_reset)
  2294. peer_flag_modify_action (peer, flag);
  2295. return 0;
  2296. }
  2297. /* peer-group member updates. */
  2298. group = peer->group;
  2299. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2300. {
  2301. if (set && CHECK_FLAG (peer->flags, flag) == flag)
  2302. continue;
  2303. if (! set && ! CHECK_FLAG (peer->flags, flag))
  2304. continue;
  2305. if (set)
  2306. SET_FLAG (peer->flags, flag);
  2307. else
  2308. UNSET_FLAG (peer->flags, flag);
  2309. if (action.type == peer_change_reset)
  2310. peer_flag_modify_action (peer, flag);
  2311. }
  2312. return 0;
  2313. }
  2314. int
  2315. peer_flag_set (struct peer *peer, u_int32_t flag)
  2316. {
  2317. return peer_flag_modify (peer, flag, 1);
  2318. }
  2319. int
  2320. peer_flag_unset (struct peer *peer, u_int32_t flag)
  2321. {
  2322. return peer_flag_modify (peer, flag, 0);
  2323. }
  2324. static int
  2325. peer_is_group_member (struct peer *peer, afi_t afi, safi_t safi)
  2326. {
  2327. if (peer->af_group[afi][safi])
  2328. return 1;
  2329. return 0;
  2330. }
  2331. static int
  2332. peer_af_flag_modify (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag,
  2333. int set)
  2334. {
  2335. int found;
  2336. int size;
  2337. struct listnode *node, *nnode;
  2338. struct peer_group *group;
  2339. struct peer_flag_action action;
  2340. memset (&action, 0, sizeof (struct peer_flag_action));
  2341. size = sizeof peer_af_flag_action_list / sizeof (struct peer_flag_action);
  2342. found = peer_flag_action_set (peer_af_flag_action_list, size, &action, flag);
  2343. /* No flag action is found. */
  2344. if (! found)
  2345. return BGP_ERR_INVALID_FLAG;
  2346. /* Adress family must be activated. */
  2347. if (! peer->afc[afi][safi])
  2348. return BGP_ERR_PEER_INACTIVE;
  2349. /* Not for peer-group member. */
  2350. if (action.not_for_member && peer_is_group_member (peer, afi, safi))
  2351. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2352. /* Spcecial check for reflector client. */
  2353. if (flag & PEER_FLAG_REFLECTOR_CLIENT
  2354. && peer_sort (peer) != BGP_PEER_IBGP)
  2355. return BGP_ERR_NOT_INTERNAL_PEER;
  2356. /* Spcecial check for remove-private-AS. */
  2357. if (flag & PEER_FLAG_REMOVE_PRIVATE_AS
  2358. && peer_sort (peer) == BGP_PEER_IBGP)
  2359. return BGP_ERR_REMOVE_PRIVATE_AS;
  2360. /* When unset the peer-group member's flag we have to check
  2361. peer-group configuration. */
  2362. if (! set && peer->af_group[afi][safi])
  2363. if (CHECK_FLAG (peer->group->conf->af_flags[afi][safi], flag))
  2364. return BGP_ERR_PEER_GROUP_HAS_THE_FLAG;
  2365. /* When current flag configuration is same as requested one. */
  2366. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2367. {
  2368. if (set && CHECK_FLAG (peer->af_flags[afi][safi], flag) == flag)
  2369. return 0;
  2370. if (! set && ! CHECK_FLAG (peer->af_flags[afi][safi], flag))
  2371. return 0;
  2372. }
  2373. if (set)
  2374. SET_FLAG (peer->af_flags[afi][safi], flag);
  2375. else
  2376. UNSET_FLAG (peer->af_flags[afi][safi], flag);
  2377. /* Execute action when peer is established. */
  2378. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)
  2379. && peer->status == Established)
  2380. {
  2381. if (! set && flag == PEER_FLAG_SOFT_RECONFIG)
  2382. bgp_clear_adj_in (peer, afi, safi);
  2383. else
  2384. {
  2385. if (flag == PEER_FLAG_REFLECTOR_CLIENT)
  2386. peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE;
  2387. else if (flag == PEER_FLAG_RSERVER_CLIENT)
  2388. peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE;
  2389. else if (flag == PEER_FLAG_ORF_PREFIX_SM)
  2390. peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
  2391. else if (flag == PEER_FLAG_ORF_PREFIX_RM)
  2392. peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
  2393. peer_change_action (peer, afi, safi, action.type);
  2394. }
  2395. }
  2396. /* Peer group member updates. */
  2397. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2398. {
  2399. group = peer->group;
  2400. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2401. {
  2402. if (! peer->af_group[afi][safi])
  2403. continue;
  2404. if (set && CHECK_FLAG (peer->af_flags[afi][safi], flag) == flag)
  2405. continue;
  2406. if (! set && ! CHECK_FLAG (peer->af_flags[afi][safi], flag))
  2407. continue;
  2408. if (set)
  2409. SET_FLAG (peer->af_flags[afi][safi], flag);
  2410. else
  2411. UNSET_FLAG (peer->af_flags[afi][safi], flag);
  2412. if (peer->status == Established)
  2413. {
  2414. if (! set && flag == PEER_FLAG_SOFT_RECONFIG)
  2415. bgp_clear_adj_in (peer, afi, safi);
  2416. else
  2417. {
  2418. if (flag == PEER_FLAG_REFLECTOR_CLIENT)
  2419. peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE;
  2420. else if (flag == PEER_FLAG_RSERVER_CLIENT)
  2421. peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE;
  2422. else if (flag == PEER_FLAG_ORF_PREFIX_SM)
  2423. peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
  2424. else if (flag == PEER_FLAG_ORF_PREFIX_RM)
  2425. peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
  2426. peer_change_action (peer, afi, safi, action.type);
  2427. }
  2428. }
  2429. }
  2430. }
  2431. return 0;
  2432. }
  2433. int
  2434. peer_af_flag_set (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag)
  2435. {
  2436. return peer_af_flag_modify (peer, afi, safi, flag, 1);
  2437. }
  2438. int
  2439. peer_af_flag_unset (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag)
  2440. {
  2441. return peer_af_flag_modify (peer, afi, safi, flag, 0);
  2442. }
  2443. /* EBGP multihop configuration. */
  2444. int
  2445. peer_ebgp_multihop_set (struct peer *peer, int ttl)
  2446. {
  2447. struct peer_group *group;
  2448. struct listnode *node, *nnode;
  2449. struct peer *peer1;
  2450. if (peer->sort == BGP_PEER_IBGP)
  2451. return BGP_ERR_NO_IBGP_WITH_TTLHACK;
  2452. if (peer->gtsm_hops != 0)
  2453. return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
  2454. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2455. {
  2456. group = peer->group;
  2457. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer1))
  2458. {
  2459. if (peer1->gtsm_hops != 0)
  2460. return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
  2461. }
  2462. }
  2463. peer->ttl = ttl;
  2464. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2465. {
  2466. bgp_set_socket_ttl (peer, peer->fd);
  2467. }
  2468. else
  2469. {
  2470. group = peer->group;
  2471. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2472. {
  2473. peer->ttl = ttl;
  2474. bgp_set_socket_ttl (peer, peer->fd);
  2475. }
  2476. }
  2477. return 0;
  2478. }
  2479. /* Neighbor description. */
  2480. int
  2481. peer_description_set (struct peer *peer, const char *desc)
  2482. {
  2483. if (peer->desc)
  2484. XFREE (MTYPE_PEER_DESC, peer->desc);
  2485. peer->desc = XSTRDUP (MTYPE_PEER_DESC, desc);
  2486. return 0;
  2487. }
  2488. int
  2489. peer_description_unset (struct peer *peer)
  2490. {
  2491. if (peer->desc)
  2492. XFREE (MTYPE_PEER_DESC, peer->desc);
  2493. peer->desc = NULL;
  2494. return 0;
  2495. }
  2496. /* Neighbor update-source. */
  2497. int
  2498. peer_update_source_if_set (struct peer *peer, const char *ifname)
  2499. {
  2500. struct peer_group *group;
  2501. struct listnode *node, *nnode;
  2502. if (peer->update_if)
  2503. {
  2504. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)
  2505. && strcmp (peer->update_if, ifname) == 0)
  2506. return 0;
  2507. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  2508. peer->update_if = NULL;
  2509. }
  2510. if (peer->update_source)
  2511. {
  2512. sockunion_free (peer->update_source);
  2513. peer->update_source = NULL;
  2514. }
  2515. peer->update_if = XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, ifname);
  2516. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2517. {
  2518. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2519. {
  2520. peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
  2521. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2522. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2523. }
  2524. else
  2525. BGP_EVENT_ADD (peer, BGP_Stop);
  2526. return 0;
  2527. }
  2528. /* peer-group member updates. */
  2529. group = peer->group;
  2530. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2531. {
  2532. if (peer->update_if)
  2533. {
  2534. if (strcmp (peer->update_if, ifname) == 0)
  2535. continue;
  2536. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  2537. peer->update_if = NULL;
  2538. }
  2539. if (peer->update_source)
  2540. {
  2541. sockunion_free (peer->update_source);
  2542. peer->update_source = NULL;
  2543. }
  2544. peer->update_if = XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, ifname);
  2545. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2546. {
  2547. peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
  2548. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2549. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2550. }
  2551. else
  2552. BGP_EVENT_ADD (peer, BGP_Stop);
  2553. }
  2554. return 0;
  2555. }
  2556. int
  2557. peer_update_source_addr_set (struct peer *peer, const union sockunion *su)
  2558. {
  2559. struct peer_group *group;
  2560. struct listnode *node, *nnode;
  2561. if (peer->update_source)
  2562. {
  2563. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)
  2564. && sockunion_cmp (peer->update_source, su) == 0)
  2565. return 0;
  2566. sockunion_free (peer->update_source);
  2567. peer->update_source = NULL;
  2568. }
  2569. if (peer->update_if)
  2570. {
  2571. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  2572. peer->update_if = NULL;
  2573. }
  2574. peer->update_source = sockunion_dup (su);
  2575. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2576. {
  2577. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2578. {
  2579. peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
  2580. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2581. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2582. }
  2583. else
  2584. BGP_EVENT_ADD (peer, BGP_Stop);
  2585. return 0;
  2586. }
  2587. /* peer-group member updates. */
  2588. group = peer->group;
  2589. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2590. {
  2591. if (peer->update_source)
  2592. {
  2593. if (sockunion_cmp (peer->update_source, su) == 0)
  2594. continue;
  2595. sockunion_free (peer->update_source);
  2596. peer->update_source = NULL;
  2597. }
  2598. if (peer->update_if)
  2599. {
  2600. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  2601. peer->update_if = NULL;
  2602. }
  2603. peer->update_source = sockunion_dup (su);
  2604. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2605. {
  2606. peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
  2607. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2608. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2609. }
  2610. else
  2611. BGP_EVENT_ADD (peer, BGP_Stop);
  2612. }
  2613. return 0;
  2614. }
  2615. int
  2616. peer_update_source_unset (struct peer *peer)
  2617. {
  2618. union sockunion *su;
  2619. struct peer_group *group;
  2620. struct listnode *node, *nnode;
  2621. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)
  2622. && ! peer->update_source
  2623. && ! peer->update_if)
  2624. return 0;
  2625. if (peer->update_source)
  2626. {
  2627. sockunion_free (peer->update_source);
  2628. peer->update_source = NULL;
  2629. }
  2630. if (peer->update_if)
  2631. {
  2632. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  2633. peer->update_if = NULL;
  2634. }
  2635. if (peer_group_active (peer))
  2636. {
  2637. group = peer->group;
  2638. if (group->conf->update_source)
  2639. {
  2640. su = sockunion_dup (group->conf->update_source);
  2641. peer->update_source = su;
  2642. }
  2643. else if (group->conf->update_if)
  2644. peer->update_if =
  2645. XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, group->conf->update_if);
  2646. }
  2647. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2648. {
  2649. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2650. {
  2651. peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
  2652. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2653. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2654. }
  2655. else
  2656. BGP_EVENT_ADD (peer, BGP_Stop);
  2657. return 0;
  2658. }
  2659. /* peer-group member updates. */
  2660. group = peer->group;
  2661. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2662. {
  2663. if (! peer->update_source && ! peer->update_if)
  2664. continue;
  2665. if (peer->update_source)
  2666. {
  2667. sockunion_free (peer->update_source);
  2668. peer->update_source = NULL;
  2669. }
  2670. if (peer->update_if)
  2671. {
  2672. XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
  2673. peer->update_if = NULL;
  2674. }
  2675. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  2676. {
  2677. peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
  2678. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  2679. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  2680. }
  2681. else
  2682. BGP_EVENT_ADD (peer, BGP_Stop);
  2683. }
  2684. return 0;
  2685. }
  2686. int
  2687. peer_default_originate_set (struct peer *peer, afi_t afi, safi_t safi,
  2688. const char *rmap)
  2689. {
  2690. struct peer_group *group;
  2691. struct listnode *node, *nnode;
  2692. /* Adress family must be activated. */
  2693. if (! peer->afc[afi][safi])
  2694. return BGP_ERR_PEER_INACTIVE;
  2695. /* Default originate can't be used for peer group memeber. */
  2696. if (peer_is_group_member (peer, afi, safi))
  2697. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2698. if (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)
  2699. || (rmap && ! peer->default_rmap[afi][safi].name)
  2700. || (rmap && strcmp (rmap, peer->default_rmap[afi][safi].name) != 0))
  2701. {
  2702. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE);
  2703. if (rmap)
  2704. {
  2705. if (peer->default_rmap[afi][safi].name)
  2706. free (peer->default_rmap[afi][safi].name);
  2707. peer->default_rmap[afi][safi].name = strdup (rmap);
  2708. peer->default_rmap[afi][safi].map = route_map_lookup_by_name (rmap);
  2709. }
  2710. }
  2711. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2712. {
  2713. if (peer->status == Established && peer->afc_nego[afi][safi])
  2714. bgp_default_originate (peer, afi, safi, 0);
  2715. return 0;
  2716. }
  2717. /* peer-group member updates. */
  2718. group = peer->group;
  2719. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2720. {
  2721. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE);
  2722. if (rmap)
  2723. {
  2724. if (peer->default_rmap[afi][safi].name)
  2725. free (peer->default_rmap[afi][safi].name);
  2726. peer->default_rmap[afi][safi].name = strdup (rmap);
  2727. peer->default_rmap[afi][safi].map = route_map_lookup_by_name (rmap);
  2728. }
  2729. if (peer->status == Established && peer->afc_nego[afi][safi])
  2730. bgp_default_originate (peer, afi, safi, 0);
  2731. }
  2732. return 0;
  2733. }
  2734. int
  2735. peer_default_originate_unset (struct peer *peer, afi_t afi, safi_t safi)
  2736. {
  2737. struct peer_group *group;
  2738. struct listnode *node, *nnode;
  2739. /* Adress family must be activated. */
  2740. if (! peer->afc[afi][safi])
  2741. return BGP_ERR_PEER_INACTIVE;
  2742. /* Default originate can't be used for peer group memeber. */
  2743. if (peer_is_group_member (peer, afi, safi))
  2744. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2745. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE))
  2746. {
  2747. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE);
  2748. if (peer->default_rmap[afi][safi].name)
  2749. free (peer->default_rmap[afi][safi].name);
  2750. peer->default_rmap[afi][safi].name = NULL;
  2751. peer->default_rmap[afi][safi].map = NULL;
  2752. }
  2753. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2754. {
  2755. if (peer->status == Established && peer->afc_nego[afi][safi])
  2756. bgp_default_originate (peer, afi, safi, 1);
  2757. return 0;
  2758. }
  2759. /* peer-group member updates. */
  2760. group = peer->group;
  2761. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2762. {
  2763. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE);
  2764. if (peer->default_rmap[afi][safi].name)
  2765. free (peer->default_rmap[afi][safi].name);
  2766. peer->default_rmap[afi][safi].name = NULL;
  2767. peer->default_rmap[afi][safi].map = NULL;
  2768. if (peer->status == Established && peer->afc_nego[afi][safi])
  2769. bgp_default_originate (peer, afi, safi, 1);
  2770. }
  2771. return 0;
  2772. }
  2773. int
  2774. peer_port_set (struct peer *peer, u_int16_t port)
  2775. {
  2776. peer->port = port;
  2777. return 0;
  2778. }
  2779. int
  2780. peer_port_unset (struct peer *peer)
  2781. {
  2782. peer->port = BGP_PORT_DEFAULT;
  2783. return 0;
  2784. }
  2785. /* neighbor weight. */
  2786. int
  2787. peer_weight_set (struct peer *peer, u_int16_t weight)
  2788. {
  2789. struct peer_group *group;
  2790. struct listnode *node, *nnode;
  2791. SET_FLAG (peer->config, PEER_CONFIG_WEIGHT);
  2792. peer->weight = weight;
  2793. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2794. return 0;
  2795. /* peer-group member updates. */
  2796. group = peer->group;
  2797. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2798. {
  2799. peer->weight = group->conf->weight;
  2800. }
  2801. return 0;
  2802. }
  2803. int
  2804. peer_weight_unset (struct peer *peer)
  2805. {
  2806. struct peer_group *group;
  2807. struct listnode *node, *nnode;
  2808. /* Set default weight. */
  2809. if (peer_group_active (peer))
  2810. peer->weight = peer->group->conf->weight;
  2811. else
  2812. peer->weight = 0;
  2813. UNSET_FLAG (peer->config, PEER_CONFIG_WEIGHT);
  2814. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2815. return 0;
  2816. /* peer-group member updates. */
  2817. group = peer->group;
  2818. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2819. {
  2820. peer->weight = 0;
  2821. }
  2822. return 0;
  2823. }
  2824. int
  2825. peer_timers_set (struct peer *peer, u_int32_t keepalive, u_int32_t holdtime)
  2826. {
  2827. struct peer_group *group;
  2828. struct listnode *node, *nnode;
  2829. /* Not for peer group memeber. */
  2830. if (peer_group_active (peer))
  2831. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2832. /* keepalive value check. */
  2833. if (keepalive > 65535)
  2834. return BGP_ERR_INVALID_VALUE;
  2835. /* Holdtime value check. */
  2836. if (holdtime > 65535)
  2837. return BGP_ERR_INVALID_VALUE;
  2838. /* Holdtime value must be either 0 or greater than 3. */
  2839. if (holdtime < 3 && holdtime != 0)
  2840. return BGP_ERR_INVALID_VALUE;
  2841. /* Set value to the configuration. */
  2842. SET_FLAG (peer->config, PEER_CONFIG_TIMER);
  2843. peer->holdtime = holdtime;
  2844. peer->keepalive = (keepalive < holdtime / 3 ? keepalive : holdtime / 3);
  2845. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2846. return 0;
  2847. /* peer-group member updates. */
  2848. group = peer->group;
  2849. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2850. {
  2851. SET_FLAG (peer->config, PEER_CONFIG_TIMER);
  2852. peer->holdtime = group->conf->holdtime;
  2853. peer->keepalive = group->conf->keepalive;
  2854. }
  2855. return 0;
  2856. }
  2857. int
  2858. peer_timers_unset (struct peer *peer)
  2859. {
  2860. struct peer_group *group;
  2861. struct listnode *node, *nnode;
  2862. if (peer_group_active (peer))
  2863. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2864. /* Clear configuration. */
  2865. UNSET_FLAG (peer->config, PEER_CONFIG_TIMER);
  2866. peer->keepalive = 0;
  2867. peer->holdtime = 0;
  2868. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2869. return 0;
  2870. /* peer-group member updates. */
  2871. group = peer->group;
  2872. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2873. {
  2874. UNSET_FLAG (peer->config, PEER_CONFIG_TIMER);
  2875. peer->holdtime = 0;
  2876. peer->keepalive = 0;
  2877. }
  2878. return 0;
  2879. }
  2880. int
  2881. peer_timers_connect_set (struct peer *peer, u_int32_t connect)
  2882. {
  2883. struct peer_group *group;
  2884. struct listnode *node, *nnode;
  2885. if (peer_group_active (peer))
  2886. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2887. if (connect > 65535)
  2888. return BGP_ERR_INVALID_VALUE;
  2889. /* Set value to the configuration. */
  2890. SET_FLAG (peer->config, PEER_CONFIG_CONNECT);
  2891. peer->connect = connect;
  2892. /* Set value to timer setting. */
  2893. peer->v_connect = connect;
  2894. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2895. return 0;
  2896. /* peer-group member updates. */
  2897. group = peer->group;
  2898. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2899. {
  2900. SET_FLAG (peer->config, PEER_CONFIG_CONNECT);
  2901. peer->connect = connect;
  2902. peer->v_connect = connect;
  2903. }
  2904. return 0;
  2905. }
  2906. int
  2907. peer_timers_connect_unset (struct peer *peer)
  2908. {
  2909. struct peer_group *group;
  2910. struct listnode *node, *nnode;
  2911. if (peer_group_active (peer))
  2912. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2913. /* Clear configuration. */
  2914. UNSET_FLAG (peer->config, PEER_CONFIG_CONNECT);
  2915. peer->connect = 0;
  2916. /* Set timer setting to default value. */
  2917. peer->v_connect = BGP_DEFAULT_CONNECT_RETRY;
  2918. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2919. return 0;
  2920. /* peer-group member updates. */
  2921. group = peer->group;
  2922. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2923. {
  2924. UNSET_FLAG (peer->config, PEER_CONFIG_CONNECT);
  2925. peer->connect = 0;
  2926. peer->v_connect = BGP_DEFAULT_CONNECT_RETRY;
  2927. }
  2928. return 0;
  2929. }
  2930. int
  2931. peer_advertise_interval_set (struct peer *peer, u_int32_t routeadv)
  2932. {
  2933. struct peer_group *group;
  2934. struct listnode *node, *nnode;
  2935. if (peer_group_active (peer))
  2936. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2937. if (routeadv > 600)
  2938. return BGP_ERR_INVALID_VALUE;
  2939. SET_FLAG (peer->config, PEER_CONFIG_ROUTEADV);
  2940. peer->routeadv = routeadv;
  2941. peer->v_routeadv = routeadv;
  2942. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2943. return 0;
  2944. /* peer-group member updates. */
  2945. group = peer->group;
  2946. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2947. {
  2948. SET_FLAG (peer->config, PEER_CONFIG_ROUTEADV);
  2949. peer->routeadv = routeadv;
  2950. peer->v_routeadv = routeadv;
  2951. }
  2952. return 0;
  2953. }
  2954. int
  2955. peer_advertise_interval_unset (struct peer *peer)
  2956. {
  2957. struct peer_group *group;
  2958. struct listnode *node, *nnode;
  2959. if (peer_group_active (peer))
  2960. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  2961. UNSET_FLAG (peer->config, PEER_CONFIG_ROUTEADV);
  2962. peer->routeadv = 0;
  2963. if (peer->sort == BGP_PEER_IBGP)
  2964. peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  2965. else
  2966. peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  2967. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  2968. return 0;
  2969. /* peer-group member updates. */
  2970. group = peer->group;
  2971. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  2972. {
  2973. UNSET_FLAG (peer->config, PEER_CONFIG_ROUTEADV);
  2974. peer->routeadv = 0;
  2975. if (peer->sort == BGP_PEER_IBGP)
  2976. peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV;
  2977. else
  2978. peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
  2979. }
  2980. return 0;
  2981. }
  2982. /* neighbor interface */
  2983. int
  2984. peer_interface_set (struct peer *peer, const char *str)
  2985. {
  2986. if (peer->ifname)
  2987. free (peer->ifname);
  2988. peer->ifname = strdup (str);
  2989. return 0;
  2990. }
  2991. int
  2992. peer_interface_unset (struct peer *peer)
  2993. {
  2994. if (peer->ifname)
  2995. free (peer->ifname);
  2996. peer->ifname = NULL;
  2997. return 0;
  2998. }
  2999. /* Allow-as in. */
  3000. int
  3001. peer_allowas_in_set (struct peer *peer, afi_t afi, safi_t safi, int allow_num)
  3002. {
  3003. struct peer_group *group;
  3004. struct listnode *node, *nnode;
  3005. if (allow_num < 1 || allow_num > 10)
  3006. return BGP_ERR_INVALID_VALUE;
  3007. if (peer->allowas_in[afi][safi] != allow_num)
  3008. {
  3009. peer->allowas_in[afi][safi] = allow_num;
  3010. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
  3011. peer_change_action (peer, afi, safi, peer_change_reset_in);
  3012. }
  3013. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3014. return 0;
  3015. group = peer->group;
  3016. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3017. {
  3018. if (peer->allowas_in[afi][safi] != allow_num)
  3019. {
  3020. peer->allowas_in[afi][safi] = allow_num;
  3021. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
  3022. peer_change_action (peer, afi, safi, peer_change_reset_in);
  3023. }
  3024. }
  3025. return 0;
  3026. }
  3027. int
  3028. peer_allowas_in_unset (struct peer *peer, afi_t afi, safi_t safi)
  3029. {
  3030. struct peer_group *group;
  3031. struct listnode *node, *nnode;
  3032. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
  3033. {
  3034. peer->allowas_in[afi][safi] = 0;
  3035. peer_af_flag_unset (peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
  3036. }
  3037. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3038. return 0;
  3039. group = peer->group;
  3040. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3041. {
  3042. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
  3043. {
  3044. peer->allowas_in[afi][safi] = 0;
  3045. peer_af_flag_unset (peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
  3046. }
  3047. }
  3048. return 0;
  3049. }
  3050. int
  3051. peer_local_as_set (struct peer *peer, as_t as, int no_prepend, int replace_as)
  3052. {
  3053. struct bgp *bgp = peer->bgp;
  3054. struct peer_group *group;
  3055. struct listnode *node, *nnode;
  3056. if (peer_sort (peer) != BGP_PEER_EBGP
  3057. && peer_sort (peer) != BGP_PEER_INTERNAL)
  3058. return BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP;
  3059. if (bgp->as == as)
  3060. return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS;
  3061. if (peer_group_active (peer))
  3062. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3063. if (peer->as == as)
  3064. return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS_REMOTE_AS;
  3065. if (peer->change_local_as == as &&
  3066. ((CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND) && no_prepend)
  3067. || (! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND) && ! no_prepend)) &&
  3068. ((CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) && replace_as)
  3069. || (! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) && ! replace_as)))
  3070. return 0;
  3071. peer->change_local_as = as;
  3072. if (no_prepend)
  3073. SET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  3074. else
  3075. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  3076. if (replace_as)
  3077. SET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  3078. else
  3079. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  3080. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3081. {
  3082. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3083. {
  3084. peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
  3085. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  3086. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3087. }
  3088. else
  3089. BGP_EVENT_ADD (peer, BGP_Stop);
  3090. return 0;
  3091. }
  3092. group = peer->group;
  3093. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3094. {
  3095. peer->change_local_as = as;
  3096. if (no_prepend)
  3097. SET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  3098. else
  3099. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  3100. if (replace_as)
  3101. SET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  3102. else
  3103. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  3104. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3105. {
  3106. peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
  3107. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  3108. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3109. }
  3110. else
  3111. BGP_EVENT_ADD (peer, BGP_Stop);
  3112. }
  3113. return 0;
  3114. }
  3115. int
  3116. peer_local_as_unset (struct peer *peer)
  3117. {
  3118. struct peer_group *group;
  3119. struct listnode *node, *nnode;
  3120. if (peer_group_active (peer))
  3121. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3122. if (! peer->change_local_as)
  3123. return 0;
  3124. peer->change_local_as = 0;
  3125. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  3126. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  3127. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3128. {
  3129. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3130. {
  3131. peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
  3132. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  3133. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3134. }
  3135. else
  3136. BGP_EVENT_ADD (peer, BGP_Stop);
  3137. return 0;
  3138. }
  3139. group = peer->group;
  3140. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3141. {
  3142. peer->change_local_as = 0;
  3143. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
  3144. UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
  3145. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3146. {
  3147. peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
  3148. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  3149. BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3150. }
  3151. else
  3152. BGP_EVENT_ADD (peer, BGP_Stop);
  3153. }
  3154. return 0;
  3155. }
  3156. /* Set password for authenticating with the peer. */
  3157. int
  3158. peer_password_set (struct peer *peer, const char *password)
  3159. {
  3160. struct listnode *nn, *nnode;
  3161. int len = password ? strlen(password) : 0;
  3162. int ret = BGP_SUCCESS;
  3163. if ((len < PEER_PASSWORD_MINLEN) || (len > PEER_PASSWORD_MAXLEN))
  3164. return BGP_ERR_INVALID_VALUE;
  3165. if (peer->password && strcmp (peer->password, password) == 0
  3166. && ! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3167. return 0;
  3168. if (peer->password)
  3169. XFREE (MTYPE_PEER_PASSWORD, peer->password);
  3170. peer->password = XSTRDUP (MTYPE_PEER_PASSWORD, password);
  3171. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3172. {
  3173. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3174. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3175. else
  3176. BGP_EVENT_ADD (peer, BGP_Stop);
  3177. return (bgp_md5_set (peer) >= 0) ? BGP_SUCCESS : BGP_ERR_TCPSIG_FAILED;
  3178. }
  3179. for (ALL_LIST_ELEMENTS (peer->group->peer, nn, nnode, peer))
  3180. {
  3181. if (peer->password && strcmp (peer->password, password) == 0)
  3182. continue;
  3183. if (peer->password)
  3184. XFREE (MTYPE_PEER_PASSWORD, peer->password);
  3185. peer->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
  3186. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3187. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3188. else
  3189. BGP_EVENT_ADD (peer, BGP_Stop);
  3190. if (bgp_md5_set (peer) < 0)
  3191. ret = BGP_ERR_TCPSIG_FAILED;
  3192. }
  3193. return ret;
  3194. }
  3195. int
  3196. peer_password_unset (struct peer *peer)
  3197. {
  3198. struct listnode *nn, *nnode;
  3199. if (!peer->password
  3200. && !CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3201. return 0;
  3202. if (!CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3203. {
  3204. if (peer_group_active (peer)
  3205. && peer->group->conf->password
  3206. && strcmp (peer->group->conf->password, peer->password) == 0)
  3207. return BGP_ERR_PEER_GROUP_HAS_THE_FLAG;
  3208. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3209. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3210. else
  3211. BGP_EVENT_ADD (peer, BGP_Stop);
  3212. if (peer->password)
  3213. XFREE (MTYPE_PEER_PASSWORD, peer->password);
  3214. peer->password = NULL;
  3215. bgp_md5_set (peer);
  3216. return 0;
  3217. }
  3218. XFREE (MTYPE_PEER_PASSWORD, peer->password);
  3219. peer->password = NULL;
  3220. for (ALL_LIST_ELEMENTS (peer->group->peer, nn, nnode, peer))
  3221. {
  3222. if (!peer->password)
  3223. continue;
  3224. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3225. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE);
  3226. else
  3227. BGP_EVENT_ADD (peer, BGP_Stop);
  3228. XFREE (MTYPE_PEER_PASSWORD, peer->password);
  3229. peer->password = NULL;
  3230. bgp_md5_set (peer);
  3231. }
  3232. return 0;
  3233. }
  3234. /* Set distribute list to the peer. */
  3235. int
  3236. peer_distribute_set (struct peer *peer, afi_t afi, safi_t safi, int direct,
  3237. const char *name)
  3238. {
  3239. struct bgp_filter *filter;
  3240. struct peer_group *group;
  3241. struct listnode *node, *nnode;
  3242. if (! peer->afc[afi][safi])
  3243. return BGP_ERR_PEER_INACTIVE;
  3244. if (direct != FILTER_IN && direct != FILTER_OUT)
  3245. return BGP_ERR_INVALID_VALUE;
  3246. if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi))
  3247. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3248. filter = &peer->filter[afi][safi];
  3249. if (filter->plist[direct].name)
  3250. return BGP_ERR_PEER_FILTER_CONFLICT;
  3251. if (filter->dlist[direct].name)
  3252. free (filter->dlist[direct].name);
  3253. filter->dlist[direct].name = strdup (name);
  3254. filter->dlist[direct].alist = access_list_lookup (afi, name);
  3255. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3256. return 0;
  3257. group = peer->group;
  3258. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3259. {
  3260. filter = &peer->filter[afi][safi];
  3261. if (! peer->af_group[afi][safi])
  3262. continue;
  3263. if (filter->dlist[direct].name)
  3264. free (filter->dlist[direct].name);
  3265. filter->dlist[direct].name = strdup (name);
  3266. filter->dlist[direct].alist = access_list_lookup (afi, name);
  3267. }
  3268. return 0;
  3269. }
  3270. int
  3271. peer_distribute_unset (struct peer *peer, afi_t afi, safi_t safi, int direct)
  3272. {
  3273. struct bgp_filter *filter;
  3274. struct bgp_filter *gfilter;
  3275. struct peer_group *group;
  3276. struct listnode *node, *nnode;
  3277. if (! peer->afc[afi][safi])
  3278. return BGP_ERR_PEER_INACTIVE;
  3279. if (direct != FILTER_IN && direct != FILTER_OUT)
  3280. return BGP_ERR_INVALID_VALUE;
  3281. if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi))
  3282. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3283. filter = &peer->filter[afi][safi];
  3284. /* apply peer-group filter */
  3285. if (peer->af_group[afi][safi])
  3286. {
  3287. gfilter = &peer->group->conf->filter[afi][safi];
  3288. if (gfilter->dlist[direct].name)
  3289. {
  3290. if (filter->dlist[direct].name)
  3291. free (filter->dlist[direct].name);
  3292. filter->dlist[direct].name = strdup (gfilter->dlist[direct].name);
  3293. filter->dlist[direct].alist = gfilter->dlist[direct].alist;
  3294. return 0;
  3295. }
  3296. }
  3297. if (filter->dlist[direct].name)
  3298. free (filter->dlist[direct].name);
  3299. filter->dlist[direct].name = NULL;
  3300. filter->dlist[direct].alist = NULL;
  3301. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3302. return 0;
  3303. group = peer->group;
  3304. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3305. {
  3306. filter = &peer->filter[afi][safi];
  3307. if (! peer->af_group[afi][safi])
  3308. continue;
  3309. if (filter->dlist[direct].name)
  3310. free (filter->dlist[direct].name);
  3311. filter->dlist[direct].name = NULL;
  3312. filter->dlist[direct].alist = NULL;
  3313. }
  3314. return 0;
  3315. }
  3316. /* Update distribute list. */
  3317. static void
  3318. peer_distribute_update (const char *name)
  3319. {
  3320. afi_t afi;
  3321. safi_t safi;
  3322. int direct;
  3323. struct listnode *mnode, *mnnode;
  3324. struct listnode *node, *nnode;
  3325. struct bgp *bgp;
  3326. struct peer *peer;
  3327. struct peer_group *group;
  3328. struct bgp_filter *filter;
  3329. for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp))
  3330. {
  3331. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  3332. {
  3333. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3334. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3335. {
  3336. filter = &peer->filter[afi][safi];
  3337. for (direct = FILTER_IN; direct < FILTER_MAX; direct++)
  3338. {
  3339. if (filter->dlist[direct].name)
  3340. filter->dlist[direct].alist =
  3341. access_list_lookup (afi, filter->dlist[direct].name);
  3342. else
  3343. filter->dlist[direct].alist = NULL;
  3344. }
  3345. }
  3346. }
  3347. for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group))
  3348. {
  3349. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3350. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3351. {
  3352. filter = &group->conf->filter[afi][safi];
  3353. for (direct = FILTER_IN; direct < FILTER_MAX; direct++)
  3354. {
  3355. if (filter->dlist[direct].name)
  3356. filter->dlist[direct].alist =
  3357. access_list_lookup (afi, filter->dlist[direct].name);
  3358. else
  3359. filter->dlist[direct].alist = NULL;
  3360. }
  3361. }
  3362. }
  3363. }
  3364. }
  3365. /* Set prefix list to the peer. */
  3366. int
  3367. peer_prefix_list_set (struct peer *peer, afi_t afi, safi_t safi, int direct,
  3368. const char *name)
  3369. {
  3370. struct bgp_filter *filter;
  3371. struct peer_group *group;
  3372. struct listnode *node, *nnode;
  3373. if (! peer->afc[afi][safi])
  3374. return BGP_ERR_PEER_INACTIVE;
  3375. if (direct != FILTER_IN && direct != FILTER_OUT)
  3376. return BGP_ERR_INVALID_VALUE;
  3377. if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi))
  3378. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3379. filter = &peer->filter[afi][safi];
  3380. if (filter->dlist[direct].name)
  3381. return BGP_ERR_PEER_FILTER_CONFLICT;
  3382. if (filter->plist[direct].name)
  3383. free (filter->plist[direct].name);
  3384. filter->plist[direct].name = strdup (name);
  3385. filter->plist[direct].plist = prefix_list_lookup (afi, name);
  3386. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3387. return 0;
  3388. group = peer->group;
  3389. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3390. {
  3391. filter = &peer->filter[afi][safi];
  3392. if (! peer->af_group[afi][safi])
  3393. continue;
  3394. if (filter->plist[direct].name)
  3395. free (filter->plist[direct].name);
  3396. filter->plist[direct].name = strdup (name);
  3397. filter->plist[direct].plist = prefix_list_lookup (afi, name);
  3398. }
  3399. return 0;
  3400. }
  3401. int
  3402. peer_prefix_list_unset (struct peer *peer, afi_t afi, safi_t safi, int direct)
  3403. {
  3404. struct bgp_filter *filter;
  3405. struct bgp_filter *gfilter;
  3406. struct peer_group *group;
  3407. struct listnode *node, *nnode;
  3408. if (! peer->afc[afi][safi])
  3409. return BGP_ERR_PEER_INACTIVE;
  3410. if (direct != FILTER_IN && direct != FILTER_OUT)
  3411. return BGP_ERR_INVALID_VALUE;
  3412. if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi))
  3413. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3414. filter = &peer->filter[afi][safi];
  3415. /* apply peer-group filter */
  3416. if (peer->af_group[afi][safi])
  3417. {
  3418. gfilter = &peer->group->conf->filter[afi][safi];
  3419. if (gfilter->plist[direct].name)
  3420. {
  3421. if (filter->plist[direct].name)
  3422. free (filter->plist[direct].name);
  3423. filter->plist[direct].name = strdup (gfilter->plist[direct].name);
  3424. filter->plist[direct].plist = gfilter->plist[direct].plist;
  3425. return 0;
  3426. }
  3427. }
  3428. if (filter->plist[direct].name)
  3429. free (filter->plist[direct].name);
  3430. filter->plist[direct].name = NULL;
  3431. filter->plist[direct].plist = NULL;
  3432. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3433. return 0;
  3434. group = peer->group;
  3435. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3436. {
  3437. filter = &peer->filter[afi][safi];
  3438. if (! peer->af_group[afi][safi])
  3439. continue;
  3440. if (filter->plist[direct].name)
  3441. free (filter->plist[direct].name);
  3442. filter->plist[direct].name = NULL;
  3443. filter->plist[direct].plist = NULL;
  3444. }
  3445. return 0;
  3446. }
  3447. /* Update prefix-list list. */
  3448. static void
  3449. peer_prefix_list_update (struct prefix_list *plist)
  3450. {
  3451. struct listnode *mnode, *mnnode;
  3452. struct listnode *node, *nnode;
  3453. struct bgp *bgp;
  3454. struct peer *peer;
  3455. struct peer_group *group;
  3456. struct bgp_filter *filter;
  3457. afi_t afi;
  3458. safi_t safi;
  3459. int direct;
  3460. for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp))
  3461. {
  3462. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  3463. {
  3464. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3465. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3466. {
  3467. filter = &peer->filter[afi][safi];
  3468. for (direct = FILTER_IN; direct < FILTER_MAX; direct++)
  3469. {
  3470. if (filter->plist[direct].name)
  3471. filter->plist[direct].plist =
  3472. prefix_list_lookup (afi, filter->plist[direct].name);
  3473. else
  3474. filter->plist[direct].plist = NULL;
  3475. }
  3476. }
  3477. }
  3478. for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group))
  3479. {
  3480. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3481. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3482. {
  3483. filter = &group->conf->filter[afi][safi];
  3484. for (direct = FILTER_IN; direct < FILTER_MAX; direct++)
  3485. {
  3486. if (filter->plist[direct].name)
  3487. filter->plist[direct].plist =
  3488. prefix_list_lookup (afi, filter->plist[direct].name);
  3489. else
  3490. filter->plist[direct].plist = NULL;
  3491. }
  3492. }
  3493. }
  3494. }
  3495. }
  3496. int
  3497. peer_aslist_set (struct peer *peer, afi_t afi, safi_t safi, int direct,
  3498. const char *name)
  3499. {
  3500. struct bgp_filter *filter;
  3501. struct peer_group *group;
  3502. struct listnode *node, *nnode;
  3503. if (! peer->afc[afi][safi])
  3504. return BGP_ERR_PEER_INACTIVE;
  3505. if (direct != FILTER_IN && direct != FILTER_OUT)
  3506. return BGP_ERR_INVALID_VALUE;
  3507. if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi))
  3508. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3509. filter = &peer->filter[afi][safi];
  3510. if (filter->aslist[direct].name)
  3511. free (filter->aslist[direct].name);
  3512. filter->aslist[direct].name = strdup (name);
  3513. filter->aslist[direct].aslist = as_list_lookup (name);
  3514. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3515. return 0;
  3516. group = peer->group;
  3517. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3518. {
  3519. filter = &peer->filter[afi][safi];
  3520. if (! peer->af_group[afi][safi])
  3521. continue;
  3522. if (filter->aslist[direct].name)
  3523. free (filter->aslist[direct].name);
  3524. filter->aslist[direct].name = strdup (name);
  3525. filter->aslist[direct].aslist = as_list_lookup (name);
  3526. }
  3527. return 0;
  3528. }
  3529. int
  3530. peer_aslist_unset (struct peer *peer,afi_t afi, safi_t safi, int direct)
  3531. {
  3532. struct bgp_filter *filter;
  3533. struct bgp_filter *gfilter;
  3534. struct peer_group *group;
  3535. struct listnode *node, *nnode;
  3536. if (! peer->afc[afi][safi])
  3537. return BGP_ERR_PEER_INACTIVE;
  3538. if (direct != FILTER_IN && direct != FILTER_OUT)
  3539. return BGP_ERR_INVALID_VALUE;
  3540. if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi))
  3541. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3542. filter = &peer->filter[afi][safi];
  3543. /* apply peer-group filter */
  3544. if (peer->af_group[afi][safi])
  3545. {
  3546. gfilter = &peer->group->conf->filter[afi][safi];
  3547. if (gfilter->aslist[direct].name)
  3548. {
  3549. if (filter->aslist[direct].name)
  3550. free (filter->aslist[direct].name);
  3551. filter->aslist[direct].name = strdup (gfilter->aslist[direct].name);
  3552. filter->aslist[direct].aslist = gfilter->aslist[direct].aslist;
  3553. return 0;
  3554. }
  3555. }
  3556. if (filter->aslist[direct].name)
  3557. free (filter->aslist[direct].name);
  3558. filter->aslist[direct].name = NULL;
  3559. filter->aslist[direct].aslist = NULL;
  3560. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3561. return 0;
  3562. group = peer->group;
  3563. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3564. {
  3565. filter = &peer->filter[afi][safi];
  3566. if (! peer->af_group[afi][safi])
  3567. continue;
  3568. if (filter->aslist[direct].name)
  3569. free (filter->aslist[direct].name);
  3570. filter->aslist[direct].name = NULL;
  3571. filter->aslist[direct].aslist = NULL;
  3572. }
  3573. return 0;
  3574. }
  3575. static void
  3576. peer_aslist_update (void)
  3577. {
  3578. afi_t afi;
  3579. safi_t safi;
  3580. int direct;
  3581. struct listnode *mnode, *mnnode;
  3582. struct listnode *node, *nnode;
  3583. struct bgp *bgp;
  3584. struct peer *peer;
  3585. struct peer_group *group;
  3586. struct bgp_filter *filter;
  3587. for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp))
  3588. {
  3589. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  3590. {
  3591. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3592. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3593. {
  3594. filter = &peer->filter[afi][safi];
  3595. for (direct = FILTER_IN; direct < FILTER_MAX; direct++)
  3596. {
  3597. if (filter->aslist[direct].name)
  3598. filter->aslist[direct].aslist =
  3599. as_list_lookup (filter->aslist[direct].name);
  3600. else
  3601. filter->aslist[direct].aslist = NULL;
  3602. }
  3603. }
  3604. }
  3605. for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group))
  3606. {
  3607. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  3608. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  3609. {
  3610. filter = &group->conf->filter[afi][safi];
  3611. for (direct = FILTER_IN; direct < FILTER_MAX; direct++)
  3612. {
  3613. if (filter->aslist[direct].name)
  3614. filter->aslist[direct].aslist =
  3615. as_list_lookup (filter->aslist[direct].name);
  3616. else
  3617. filter->aslist[direct].aslist = NULL;
  3618. }
  3619. }
  3620. }
  3621. }
  3622. }
  3623. /* Set route-map to the peer. */
  3624. int
  3625. peer_route_map_set (struct peer *peer, afi_t afi, safi_t safi, int direct,
  3626. const char *name)
  3627. {
  3628. struct bgp_filter *filter;
  3629. struct peer_group *group;
  3630. struct listnode *node, *nnode;
  3631. if (! peer->afc[afi][safi])
  3632. return BGP_ERR_PEER_INACTIVE;
  3633. if (direct != RMAP_IN && direct != RMAP_OUT &&
  3634. direct != RMAP_IMPORT && direct != RMAP_EXPORT)
  3635. return BGP_ERR_INVALID_VALUE;
  3636. if ( (direct == RMAP_OUT || direct == RMAP_IMPORT)
  3637. && peer_is_group_member (peer, afi, safi))
  3638. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3639. filter = &peer->filter[afi][safi];
  3640. if (filter->map[direct].name)
  3641. free (filter->map[direct].name);
  3642. filter->map[direct].name = strdup (name);
  3643. filter->map[direct].map = route_map_lookup_by_name (name);
  3644. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3645. return 0;
  3646. group = peer->group;
  3647. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3648. {
  3649. filter = &peer->filter[afi][safi];
  3650. if (! peer->af_group[afi][safi])
  3651. continue;
  3652. if (filter->map[direct].name)
  3653. free (filter->map[direct].name);
  3654. filter->map[direct].name = strdup (name);
  3655. filter->map[direct].map = route_map_lookup_by_name (name);
  3656. }
  3657. return 0;
  3658. }
  3659. /* Unset route-map from the peer. */
  3660. int
  3661. peer_route_map_unset (struct peer *peer, afi_t afi, safi_t safi, int direct)
  3662. {
  3663. struct bgp_filter *filter;
  3664. struct bgp_filter *gfilter;
  3665. struct peer_group *group;
  3666. struct listnode *node, *nnode;
  3667. if (! peer->afc[afi][safi])
  3668. return BGP_ERR_PEER_INACTIVE;
  3669. if (direct != RMAP_IN && direct != RMAP_OUT &&
  3670. direct != RMAP_IMPORT && direct != RMAP_EXPORT)
  3671. return BGP_ERR_INVALID_VALUE;
  3672. if ( (direct == RMAP_OUT || direct == RMAP_IMPORT)
  3673. && peer_is_group_member (peer, afi, safi))
  3674. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3675. filter = &peer->filter[afi][safi];
  3676. /* apply peer-group filter */
  3677. if (peer->af_group[afi][safi])
  3678. {
  3679. gfilter = &peer->group->conf->filter[afi][safi];
  3680. if (gfilter->map[direct].name)
  3681. {
  3682. if (filter->map[direct].name)
  3683. free (filter->map[direct].name);
  3684. filter->map[direct].name = strdup (gfilter->map[direct].name);
  3685. filter->map[direct].map = gfilter->map[direct].map;
  3686. return 0;
  3687. }
  3688. }
  3689. if (filter->map[direct].name)
  3690. free (filter->map[direct].name);
  3691. filter->map[direct].name = NULL;
  3692. filter->map[direct].map = NULL;
  3693. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3694. return 0;
  3695. group = peer->group;
  3696. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3697. {
  3698. filter = &peer->filter[afi][safi];
  3699. if (! peer->af_group[afi][safi])
  3700. continue;
  3701. if (filter->map[direct].name)
  3702. free (filter->map[direct].name);
  3703. filter->map[direct].name = NULL;
  3704. filter->map[direct].map = NULL;
  3705. }
  3706. return 0;
  3707. }
  3708. /* Set unsuppress-map to the peer. */
  3709. int
  3710. peer_unsuppress_map_set (struct peer *peer, afi_t afi, safi_t safi,
  3711. const char *name)
  3712. {
  3713. struct bgp_filter *filter;
  3714. struct peer_group *group;
  3715. struct listnode *node, *nnode;
  3716. if (! peer->afc[afi][safi])
  3717. return BGP_ERR_PEER_INACTIVE;
  3718. if (peer_is_group_member (peer, afi, safi))
  3719. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3720. filter = &peer->filter[afi][safi];
  3721. if (filter->usmap.name)
  3722. free (filter->usmap.name);
  3723. filter->usmap.name = strdup (name);
  3724. filter->usmap.map = route_map_lookup_by_name (name);
  3725. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3726. return 0;
  3727. group = peer->group;
  3728. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3729. {
  3730. filter = &peer->filter[afi][safi];
  3731. if (! peer->af_group[afi][safi])
  3732. continue;
  3733. if (filter->usmap.name)
  3734. free (filter->usmap.name);
  3735. filter->usmap.name = strdup (name);
  3736. filter->usmap.map = route_map_lookup_by_name (name);
  3737. }
  3738. return 0;
  3739. }
  3740. /* Unset route-map from the peer. */
  3741. int
  3742. peer_unsuppress_map_unset (struct peer *peer, afi_t afi, safi_t safi)
  3743. {
  3744. struct bgp_filter *filter;
  3745. struct peer_group *group;
  3746. struct listnode *node, *nnode;
  3747. if (! peer->afc[afi][safi])
  3748. return BGP_ERR_PEER_INACTIVE;
  3749. if (peer_is_group_member (peer, afi, safi))
  3750. return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER;
  3751. filter = &peer->filter[afi][safi];
  3752. if (filter->usmap.name)
  3753. free (filter->usmap.name);
  3754. filter->usmap.name = NULL;
  3755. filter->usmap.map = NULL;
  3756. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3757. return 0;
  3758. group = peer->group;
  3759. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3760. {
  3761. filter = &peer->filter[afi][safi];
  3762. if (! peer->af_group[afi][safi])
  3763. continue;
  3764. if (filter->usmap.name)
  3765. free (filter->usmap.name);
  3766. filter->usmap.name = NULL;
  3767. filter->usmap.map = NULL;
  3768. }
  3769. return 0;
  3770. }
  3771. int
  3772. peer_maximum_prefix_set (struct peer *peer, afi_t afi, safi_t safi,
  3773. u_int32_t max, u_char threshold,
  3774. int warning, u_int16_t restart)
  3775. {
  3776. struct peer_group *group;
  3777. struct listnode *node, *nnode;
  3778. if (! peer->afc[afi][safi])
  3779. return BGP_ERR_PEER_INACTIVE;
  3780. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX);
  3781. peer->pmax[afi][safi] = max;
  3782. peer->pmax_threshold[afi][safi] = threshold;
  3783. peer->pmax_restart[afi][safi] = restart;
  3784. if (warning)
  3785. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3786. else
  3787. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3788. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3789. {
  3790. group = peer->group;
  3791. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3792. {
  3793. if (! peer->af_group[afi][safi])
  3794. continue;
  3795. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX);
  3796. peer->pmax[afi][safi] = max;
  3797. peer->pmax_threshold[afi][safi] = threshold;
  3798. peer->pmax_restart[afi][safi] = restart;
  3799. if (warning)
  3800. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3801. else
  3802. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3803. if ((peer->status == Established) && (peer->afc[afi][safi]))
  3804. bgp_maximum_prefix_overflow (peer, afi, safi, 1);
  3805. }
  3806. }
  3807. else
  3808. {
  3809. if ((peer->status == Established) && (peer->afc[afi][safi]))
  3810. bgp_maximum_prefix_overflow (peer, afi, safi, 1);
  3811. }
  3812. return 0;
  3813. }
  3814. int
  3815. peer_maximum_prefix_unset (struct peer *peer, afi_t afi, safi_t safi)
  3816. {
  3817. struct peer_group *group;
  3818. struct listnode *node, *nnode;
  3819. if (! peer->afc[afi][safi])
  3820. return BGP_ERR_PEER_INACTIVE;
  3821. /* apply peer-group config */
  3822. if (peer->af_group[afi][safi])
  3823. {
  3824. if (CHECK_FLAG (peer->group->conf->af_flags[afi][safi],
  3825. PEER_FLAG_MAX_PREFIX))
  3826. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX);
  3827. else
  3828. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX);
  3829. if (CHECK_FLAG (peer->group->conf->af_flags[afi][safi],
  3830. PEER_FLAG_MAX_PREFIX_WARNING))
  3831. SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3832. else
  3833. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3834. peer->pmax[afi][safi] = peer->group->conf->pmax[afi][safi];
  3835. peer->pmax_threshold[afi][safi] = peer->group->conf->pmax_threshold[afi][safi];
  3836. peer->pmax_restart[afi][safi] = peer->group->conf->pmax_restart[afi][safi];
  3837. return 0;
  3838. }
  3839. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX);
  3840. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3841. peer->pmax[afi][safi] = 0;
  3842. peer->pmax_threshold[afi][safi] = 0;
  3843. peer->pmax_restart[afi][safi] = 0;
  3844. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3845. return 0;
  3846. group = peer->group;
  3847. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3848. {
  3849. if (! peer->af_group[afi][safi])
  3850. continue;
  3851. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX);
  3852. UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING);
  3853. peer->pmax[afi][safi] = 0;
  3854. peer->pmax_threshold[afi][safi] = 0;
  3855. peer->pmax_restart[afi][safi] = 0;
  3856. }
  3857. return 0;
  3858. }
  3859. /* Set # of hops between us and BGP peer. */
  3860. int
  3861. peer_ttl_security_hops_set (struct peer *peer, int gtsm_hops)
  3862. {
  3863. struct peer_group *group;
  3864. struct listnode *node, *nnode;
  3865. struct peer *peer1;
  3866. zlog_debug ("peer_ttl_security_hops_set: set gtsm_hops to %d for %s", gtsm_hops, peer->host);
  3867. if (peer->ttl != 0)
  3868. return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
  3869. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3870. {
  3871. group = peer->group;
  3872. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer1))
  3873. {
  3874. if (peer1->ttl != 0)
  3875. return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
  3876. }
  3877. }
  3878. peer->gtsm_hops = gtsm_hops;
  3879. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  3880. {
  3881. bgp_set_socket_ttl (peer, peer->fd);
  3882. }
  3883. else
  3884. {
  3885. group = peer->group;
  3886. for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
  3887. {
  3888. peer->gtsm_hops = gtsm_hops;
  3889. /* Change setting of existing peer
  3890. * established then change value (may break connectivity)
  3891. * not established yet (teardown session and restart)
  3892. * no session then do nothing (will get handled by next connection)
  3893. */
  3894. if (peer->status == Established)
  3895. {
  3896. bgp_set_socket_ttl (peer, peer->fd);
  3897. }
  3898. else if (peer->status < Established)
  3899. {
  3900. if (BGP_DEBUG (events, EVENTS))
  3901. zlog_debug ("%s Min-ttl changed", peer->host);
  3902. BGP_EVENT_ADD (peer, BGP_Stop);
  3903. }
  3904. }
  3905. }
  3906. return 0;
  3907. }
  3908. int
  3909. peer_clear (struct peer *peer)
  3910. {
  3911. if (! CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN))
  3912. {
  3913. if (CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
  3914. {
  3915. UNSET_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
  3916. if (peer->t_pmax_restart)
  3917. {
  3918. BGP_TIMER_OFF (peer->t_pmax_restart);
  3919. if (BGP_DEBUG (events, EVENTS))
  3920. zlog_debug ("%s Maximum-prefix restart timer canceled",
  3921. peer->host);
  3922. }
  3923. BGP_EVENT_ADD (peer, BGP_Start);
  3924. return 0;
  3925. }
  3926. peer->v_start = BGP_INIT_START_TIMER;
  3927. if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
  3928. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  3929. BGP_NOTIFY_CEASE_ADMIN_RESET);
  3930. else
  3931. BGP_EVENT_ADD (peer, BGP_Stop);
  3932. }
  3933. return 0;
  3934. }
  3935. int
  3936. peer_clear_soft (struct peer *peer, afi_t afi, safi_t safi,
  3937. enum bgp_clear_type stype)
  3938. {
  3939. if (peer->status != Established)
  3940. return 0;
  3941. if (! peer->afc[afi][safi])
  3942. return BGP_ERR_AF_UNCONFIGURED;
  3943. peer->rtt = sockopt_tcp_rtt (peer->fd);
  3944. if (stype == BGP_CLEAR_SOFT_RSCLIENT)
  3945. {
  3946. if (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
  3947. return 0;
  3948. bgp_check_local_routes_rsclient (peer, afi, safi);
  3949. bgp_soft_reconfig_rsclient (peer, afi, safi);
  3950. }
  3951. if (stype == BGP_CLEAR_SOFT_OUT || stype == BGP_CLEAR_SOFT_BOTH)
  3952. bgp_announce_route (peer, afi, safi);
  3953. if (stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX)
  3954. {
  3955. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV)
  3956. && (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV)
  3957. || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV)))
  3958. {
  3959. struct bgp_filter *filter = &peer->filter[afi][safi];
  3960. u_char prefix_type;
  3961. if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
  3962. prefix_type = ORF_TYPE_PREFIX;
  3963. else
  3964. prefix_type = ORF_TYPE_PREFIX_OLD;
  3965. if (filter->plist[FILTER_IN].plist)
  3966. {
  3967. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND))
  3968. bgp_route_refresh_send (peer, afi, safi,
  3969. prefix_type, REFRESH_DEFER, 1);
  3970. bgp_route_refresh_send (peer, afi, safi, prefix_type,
  3971. REFRESH_IMMEDIATE, 0);
  3972. }
  3973. else
  3974. {
  3975. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND))
  3976. bgp_route_refresh_send (peer, afi, safi,
  3977. prefix_type, REFRESH_IMMEDIATE, 1);
  3978. else
  3979. bgp_route_refresh_send (peer, afi, safi, 0, 0, 0);
  3980. }
  3981. return 0;
  3982. }
  3983. }
  3984. if (stype == BGP_CLEAR_SOFT_IN || stype == BGP_CLEAR_SOFT_BOTH
  3985. || stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX)
  3986. {
  3987. /* If neighbor has soft reconfiguration inbound flag.
  3988. Use Adj-RIB-In database. */
  3989. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG))
  3990. bgp_soft_reconfig_in (peer, afi, safi);
  3991. else
  3992. {
  3993. /* If neighbor has route refresh capability, send route refresh
  3994. message to the peer. */
  3995. if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV)
  3996. || CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
  3997. bgp_route_refresh_send (peer, afi, safi, 0, 0, 0);
  3998. else
  3999. return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED;
  4000. }
  4001. }
  4002. return 0;
  4003. }
  4004. /* Display peer uptime.*/
  4005. /* XXX: why does this function return char * when it takes buffer? */
  4006. char *
  4007. peer_uptime (time_t uptime2, char *buf, size_t len)
  4008. {
  4009. time_t uptime1;
  4010. struct tm *tm;
  4011. /* Check buffer length. */
  4012. if (len < BGP_UPTIME_LEN)
  4013. {
  4014. zlog_warn ("peer_uptime (): buffer shortage %lu", (u_long)len);
  4015. /* XXX: should return status instead of buf... */
  4016. snprintf (buf, len, "<error> ");
  4017. return buf;
  4018. }
  4019. /* If there is no connection has been done before print `never'. */
  4020. if (uptime2 == 0)
  4021. {
  4022. snprintf (buf, len, "never ");
  4023. return buf;
  4024. }
  4025. /* Get current time. */
  4026. uptime1 = bgp_clock ();
  4027. uptime1 -= uptime2;
  4028. tm = gmtime (&uptime1);
  4029. /* Making formatted timer strings. */
  4030. #define ONE_DAY_SECOND 60*60*24
  4031. #define ONE_WEEK_SECOND ONE_DAY_SECOND*7
  4032. #define ONE_YEAR_SECOND ONE_DAY_SECOND*365
  4033. if (uptime1 < ONE_DAY_SECOND)
  4034. snprintf (buf, len, "%02d:%02d:%02d",
  4035. tm->tm_hour, tm->tm_min, tm->tm_sec);
  4036. else if (uptime1 < ONE_WEEK_SECOND)
  4037. snprintf (buf, len, "%dd%02dh%02dm",
  4038. tm->tm_yday, tm->tm_hour, tm->tm_min);
  4039. else if (uptime1 < ONE_YEAR_SECOND)
  4040. snprintf (buf, len, "%02dw%dd%02dh",
  4041. tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
  4042. else
  4043. snprintf (buf, len, "%02dy%02dw%dd",
  4044. tm->tm_year - 70, tm->tm_yday/7,
  4045. tm->tm_yday - ((tm->tm_yday/7) * 7));
  4046. return buf;
  4047. }
  4048. static void
  4049. bgp_config_write_filter (struct vty *vty, struct peer *peer,
  4050. afi_t afi, safi_t safi)
  4051. {
  4052. struct bgp_filter *filter;
  4053. struct bgp_filter *gfilter = NULL;
  4054. char *addr;
  4055. int in = FILTER_IN;
  4056. int out = FILTER_OUT;
  4057. addr = peer->host;
  4058. filter = &peer->filter[afi][safi];
  4059. if (peer->af_group[afi][safi])
  4060. gfilter = &peer->group->conf->filter[afi][safi];
  4061. /* distribute-list. */
  4062. if (filter->dlist[in].name)
  4063. if (! gfilter || ! gfilter->dlist[in].name
  4064. || strcmp (filter->dlist[in].name, gfilter->dlist[in].name) != 0)
  4065. vty_out (vty, " neighbor %s distribute-list %s in%s", addr,
  4066. filter->dlist[in].name, VTY_NEWLINE);
  4067. if (filter->dlist[out].name && ! gfilter)
  4068. vty_out (vty, " neighbor %s distribute-list %s out%s", addr,
  4069. filter->dlist[out].name, VTY_NEWLINE);
  4070. /* prefix-list. */
  4071. if (filter->plist[in].name)
  4072. if (! gfilter || ! gfilter->plist[in].name
  4073. || strcmp (filter->plist[in].name, gfilter->plist[in].name) != 0)
  4074. vty_out (vty, " neighbor %s prefix-list %s in%s", addr,
  4075. filter->plist[in].name, VTY_NEWLINE);
  4076. if (filter->plist[out].name && ! gfilter)
  4077. vty_out (vty, " neighbor %s prefix-list %s out%s", addr,
  4078. filter->plist[out].name, VTY_NEWLINE);
  4079. /* route-map. */
  4080. if (filter->map[RMAP_IN].name)
  4081. if (! gfilter || ! gfilter->map[RMAP_IN].name
  4082. || strcmp (filter->map[RMAP_IN].name, gfilter->map[RMAP_IN].name) != 0)
  4083. vty_out (vty, " neighbor %s route-map %s in%s", addr,
  4084. filter->map[RMAP_IN].name, VTY_NEWLINE);
  4085. if (filter->map[RMAP_OUT].name && ! gfilter)
  4086. vty_out (vty, " neighbor %s route-map %s out%s", addr,
  4087. filter->map[RMAP_OUT].name, VTY_NEWLINE);
  4088. if (filter->map[RMAP_IMPORT].name && ! gfilter)
  4089. vty_out (vty, " neighbor %s route-map %s import%s", addr,
  4090. filter->map[RMAP_IMPORT].name, VTY_NEWLINE);
  4091. if (filter->map[RMAP_EXPORT].name)
  4092. if (! gfilter || ! gfilter->map[RMAP_EXPORT].name
  4093. || strcmp (filter->map[RMAP_EXPORT].name,
  4094. gfilter->map[RMAP_EXPORT].name) != 0)
  4095. vty_out (vty, " neighbor %s route-map %s export%s", addr,
  4096. filter->map[RMAP_EXPORT].name, VTY_NEWLINE);
  4097. /* unsuppress-map */
  4098. if (filter->usmap.name && ! gfilter)
  4099. vty_out (vty, " neighbor %s unsuppress-map %s%s", addr,
  4100. filter->usmap.name, VTY_NEWLINE);
  4101. /* filter-list. */
  4102. if (filter->aslist[in].name)
  4103. if (! gfilter || ! gfilter->aslist[in].name
  4104. || strcmp (filter->aslist[in].name, gfilter->aslist[in].name) != 0)
  4105. vty_out (vty, " neighbor %s filter-list %s in%s", addr,
  4106. filter->aslist[in].name, VTY_NEWLINE);
  4107. if (filter->aslist[out].name && ! gfilter)
  4108. vty_out (vty, " neighbor %s filter-list %s out%s", addr,
  4109. filter->aslist[out].name, VTY_NEWLINE);
  4110. }
  4111. /* BGP peer configuration display function. */
  4112. static void
  4113. bgp_config_write_peer (struct vty *vty, struct bgp *bgp,
  4114. struct peer *peer, afi_t afi, safi_t safi)
  4115. {
  4116. struct peer *g_peer = NULL;
  4117. char buf[SU_ADDRSTRLEN];
  4118. char *addr;
  4119. addr = peer->host;
  4120. if (peer_group_active (peer))
  4121. g_peer = peer->group->conf;
  4122. /************************************
  4123. ****** Global to the neighbor ******
  4124. ************************************/
  4125. if (afi == AFI_IP && safi == SAFI_UNICAST)
  4126. {
  4127. /* remote-as. */
  4128. if (! peer_group_active (peer))
  4129. {
  4130. if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
  4131. vty_out (vty, " neighbor %s peer-group%s", addr,
  4132. VTY_NEWLINE);
  4133. if (peer->as)
  4134. vty_out (vty, " neighbor %s remote-as %u%s", addr, peer->as,
  4135. VTY_NEWLINE);
  4136. }
  4137. else
  4138. {
  4139. if (! g_peer->as)
  4140. vty_out (vty, " neighbor %s remote-as %u%s", addr, peer->as,
  4141. VTY_NEWLINE);
  4142. if (peer->af_group[AFI_IP][SAFI_UNICAST])
  4143. vty_out (vty, " neighbor %s peer-group %s%s", addr,
  4144. peer->group->name, VTY_NEWLINE);
  4145. }
  4146. /* local-as. */
  4147. if (peer->change_local_as)
  4148. if (! peer_group_active (peer))
  4149. vty_out (vty, " neighbor %s local-as %u%s%s%s", addr,
  4150. peer->change_local_as,
  4151. CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND) ?
  4152. " no-prepend" : "",
  4153. CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) ?
  4154. " replace-as" : "", VTY_NEWLINE);
  4155. /* Description. */
  4156. if (peer->desc)
  4157. vty_out (vty, " neighbor %s description %s%s", addr, peer->desc,
  4158. VTY_NEWLINE);
  4159. /* Shutdown. */
  4160. if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN))
  4161. if (! peer_group_active (peer) ||
  4162. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_SHUTDOWN))
  4163. vty_out (vty, " neighbor %s shutdown%s", addr, VTY_NEWLINE);
  4164. /* Password. */
  4165. if (peer->password)
  4166. if (!peer_group_active (peer)
  4167. || ! g_peer->password
  4168. || strcmp (peer->password, g_peer->password) != 0)
  4169. vty_out (vty, " neighbor %s password %s%s", addr, peer->password,
  4170. VTY_NEWLINE);
  4171. /* BGP port. */
  4172. if (peer->port != BGP_PORT_DEFAULT)
  4173. vty_out (vty, " neighbor %s port %d%s", addr, peer->port,
  4174. VTY_NEWLINE);
  4175. /* Local interface name. */
  4176. if (peer->ifname)
  4177. vty_out (vty, " neighbor %s interface %s%s", addr, peer->ifname,
  4178. VTY_NEWLINE);
  4179. /* Passive. */
  4180. if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
  4181. if (! peer_group_active (peer) ||
  4182. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_PASSIVE))
  4183. vty_out (vty, " neighbor %s passive%s", addr, VTY_NEWLINE);
  4184. /* TTL option */
  4185. if (peer->gtsm_hops && ! peer_group_active (peer))
  4186. vty_out (vty, " neighbor %s ttl-security hops %d%s", addr,
  4187. peer->gtsm_hops, VTY_NEWLINE);
  4188. else if (peer->ttl && ! peer_group_active (peer))
  4189. vty_out (vty, " neighbor %s ebgp-multihop %d%s", addr, peer->ttl,
  4190. VTY_NEWLINE);
  4191. /* disable-connected-check. */
  4192. if (CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  4193. if (! peer_group_active (peer) ||
  4194. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  4195. vty_out (vty, " neighbor %s disable-connected-check%s", addr, VTY_NEWLINE);
  4196. /* Update-source. */
  4197. if (peer->update_if)
  4198. if (! peer_group_active (peer) || ! g_peer->update_if
  4199. || strcmp (g_peer->update_if, peer->update_if) != 0)
  4200. vty_out (vty, " neighbor %s update-source %s%s", addr,
  4201. peer->update_if, VTY_NEWLINE);
  4202. if (peer->update_source)
  4203. if (! peer_group_active (peer) || ! g_peer->update_source
  4204. || sockunion_cmp (g_peer->update_source,
  4205. peer->update_source) != 0)
  4206. vty_out (vty, " neighbor %s update-source %s%s", addr,
  4207. sockunion2str (peer->update_source, buf, SU_ADDRSTRLEN),
  4208. VTY_NEWLINE);
  4209. /* advertisement-interval */
  4210. if (CHECK_FLAG (peer->config, PEER_CONFIG_ROUTEADV) &&
  4211. ! peer_group_active (peer))
  4212. vty_out (vty, " neighbor %s advertisement-interval %d%s",
  4213. addr, peer->v_routeadv, VTY_NEWLINE);
  4214. /* timers. */
  4215. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)
  4216. && ! peer_group_active (peer))
  4217. vty_out (vty, " neighbor %s timers %d %d%s", addr,
  4218. peer->keepalive, peer->holdtime, VTY_NEWLINE);
  4219. if (CHECK_FLAG (peer->config, PEER_CONFIG_CONNECT) &&
  4220. ! peer_group_active (peer))
  4221. vty_out (vty, " neighbor %s timers connect %d%s", addr,
  4222. peer->connect, VTY_NEWLINE);
  4223. /* Default weight. */
  4224. if (CHECK_FLAG (peer->config, PEER_CONFIG_WEIGHT))
  4225. if (! peer_group_active (peer) ||
  4226. g_peer->weight != peer->weight)
  4227. vty_out (vty, " neighbor %s weight %d%s", addr, peer->weight,
  4228. VTY_NEWLINE);
  4229. /* Dynamic capability. */
  4230. if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY))
  4231. if (! peer_group_active (peer) ||
  4232. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY))
  4233. vty_out (vty, " neighbor %s capability dynamic%s", addr,
  4234. VTY_NEWLINE);
  4235. /* dont capability negotiation. */
  4236. if (CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY))
  4237. if (! peer_group_active (peer) ||
  4238. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_DONT_CAPABILITY))
  4239. vty_out (vty, " neighbor %s dont-capability-negotiate%s", addr,
  4240. VTY_NEWLINE);
  4241. /* override capability negotiation. */
  4242. if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
  4243. if (! peer_group_active (peer) ||
  4244. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
  4245. vty_out (vty, " neighbor %s override-capability%s", addr,
  4246. VTY_NEWLINE);
  4247. /* strict capability negotiation. */
  4248. if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH))
  4249. if (! peer_group_active (peer) ||
  4250. ! CHECK_FLAG (g_peer->flags, PEER_FLAG_STRICT_CAP_MATCH))
  4251. vty_out (vty, " neighbor %s strict-capability-match%s", addr,
  4252. VTY_NEWLINE);
  4253. if (! peer->af_group[AFI_IP][SAFI_UNICAST])
  4254. {
  4255. if (bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4))
  4256. {
  4257. if (peer->afc[AFI_IP][SAFI_UNICAST])
  4258. vty_out (vty, " neighbor %s activate%s", addr, VTY_NEWLINE);
  4259. }
  4260. else
  4261. {
  4262. if (! peer->afc[AFI_IP][SAFI_UNICAST])
  4263. vty_out (vty, " no neighbor %s activate%s", addr, VTY_NEWLINE);
  4264. }
  4265. }
  4266. }
  4267. /************************************
  4268. ****** Per AF to the neighbor ******
  4269. ************************************/
  4270. if (! (afi == AFI_IP && safi == SAFI_UNICAST))
  4271. {
  4272. if (peer->af_group[afi][safi])
  4273. vty_out (vty, " neighbor %s peer-group %s%s", addr,
  4274. peer->group->name, VTY_NEWLINE);
  4275. else
  4276. vty_out (vty, " neighbor %s activate%s", addr, VTY_NEWLINE);
  4277. }
  4278. /* ORF capability. */
  4279. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
  4280. || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
  4281. if (! peer->af_group[afi][safi])
  4282. {
  4283. vty_out (vty, " neighbor %s capability orf prefix-list", addr);
  4284. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
  4285. && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
  4286. vty_out (vty, " both");
  4287. else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM))
  4288. vty_out (vty, " send");
  4289. else
  4290. vty_out (vty, " receive");
  4291. vty_out (vty, "%s", VTY_NEWLINE);
  4292. }
  4293. /* Route reflector client. */
  4294. if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_REFLECTOR_CLIENT)
  4295. && ! peer->af_group[afi][safi])
  4296. vty_out (vty, " neighbor %s route-reflector-client%s", addr,
  4297. VTY_NEWLINE);
  4298. /* Nexthop self. */
  4299. if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_NEXTHOP_SELF)
  4300. && ! peer->af_group[afi][safi])
  4301. vty_out (vty, " neighbor %s next-hop-self%s%s", addr,
  4302. peer_af_flag_check (peer, afi, safi, PEER_FLAG_NEXTHOP_SELF_ALL) ?
  4303. " all" : "", VTY_NEWLINE);
  4304. /* Remove private AS. */
  4305. if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_REMOVE_PRIVATE_AS)
  4306. && ! peer->af_group[afi][safi])
  4307. vty_out (vty, " neighbor %s remove-private-AS%s",
  4308. addr, VTY_NEWLINE);
  4309. /* send-community print. */
  4310. if (! peer->af_group[afi][safi])
  4311. {
  4312. if (bgp_option_check (BGP_OPT_CONFIG_CISCO))
  4313. {
  4314. if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY)
  4315. && peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY)
  4316. && peer_af_flag_check(peer, afi, safi, PEER_FLAG_SEND_LARGE_COMMUNITY))
  4317. vty_out (vty, " neighbor %s send-community all%s", addr, VTY_NEWLINE);
  4318. else if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY))
  4319. vty_out (vty, " neighbor %s send-community extended%s",
  4320. addr, VTY_NEWLINE);
  4321. else if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_LARGE_COMMUNITY))
  4322. vty_out (vty, " neighbor %s send-community large%s",
  4323. addr, VTY_NEWLINE);
  4324. else if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY))
  4325. vty_out (vty, " neighbor %s send-community%s", addr, VTY_NEWLINE);
  4326. }
  4327. else
  4328. {
  4329. if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY)
  4330. && ! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY)
  4331. && ! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_LARGE_COMMUNITY))
  4332. vty_out (vty, " no neighbor %s send-community all%s",
  4333. addr, VTY_NEWLINE);
  4334. else if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY))
  4335. vty_out (vty, " no neighbor %s send-community extended%s",
  4336. addr, VTY_NEWLINE);
  4337. else if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_LARGE_COMMUNITY))
  4338. vty_out (vty, " no neighbor %s send-community large%s",
  4339. addr, VTY_NEWLINE);
  4340. else if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY))
  4341. vty_out (vty, " no neighbor %s send-community%s",
  4342. addr, VTY_NEWLINE);
  4343. }
  4344. }
  4345. /* Default information */
  4346. if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_DEFAULT_ORIGINATE)
  4347. && ! peer->af_group[afi][safi])
  4348. {
  4349. vty_out (vty, " neighbor %s default-originate", addr);
  4350. if (peer->default_rmap[afi][safi].name)
  4351. vty_out (vty, " route-map %s", peer->default_rmap[afi][safi].name);
  4352. vty_out (vty, "%s", VTY_NEWLINE);
  4353. }
  4354. /* Soft reconfiguration inbound. */
  4355. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG))
  4356. if (! peer->af_group[afi][safi] ||
  4357. ! CHECK_FLAG (g_peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG))
  4358. vty_out (vty, " neighbor %s soft-reconfiguration inbound%s", addr,
  4359. VTY_NEWLINE);
  4360. /* maximum-prefix. */
  4361. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
  4362. if (! peer->af_group[afi][safi]
  4363. || g_peer->pmax[afi][safi] != peer->pmax[afi][safi]
  4364. || g_peer->pmax_threshold[afi][safi] != peer->pmax_threshold[afi][safi]
  4365. || CHECK_FLAG (g_peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING)
  4366. != CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING))
  4367. {
  4368. vty_out (vty, " neighbor %s maximum-prefix %ld", addr, peer->pmax[afi][safi]);
  4369. if (peer->pmax_threshold[afi][safi] != MAXIMUM_PREFIX_THRESHOLD_DEFAULT)
  4370. vty_out (vty, " %d", peer->pmax_threshold[afi][safi]);
  4371. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING))
  4372. vty_out (vty, " warning-only");
  4373. if (peer->pmax_restart[afi][safi])
  4374. vty_out (vty, " restart %d", peer->pmax_restart[afi][safi]);
  4375. vty_out (vty, "%s", VTY_NEWLINE);
  4376. }
  4377. /* Route server client. */
  4378. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
  4379. && ! peer->af_group[afi][safi])
  4380. vty_out (vty, " neighbor %s route-server-client%s", addr, VTY_NEWLINE);
  4381. /* Nexthop-local unchanged. */
  4382. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)
  4383. && ! peer->af_group[afi][safi])
  4384. vty_out (vty, " neighbor %s nexthop-local unchanged%s", addr, VTY_NEWLINE);
  4385. /* Allow AS in. */
  4386. if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_ALLOWAS_IN))
  4387. if (! peer_group_active (peer)
  4388. || ! peer_af_flag_check (g_peer, afi, safi, PEER_FLAG_ALLOWAS_IN)
  4389. || peer->allowas_in[afi][safi] != g_peer->allowas_in[afi][safi])
  4390. {
  4391. if (peer->allowas_in[afi][safi] == 3)
  4392. vty_out (vty, " neighbor %s allowas-in%s", addr, VTY_NEWLINE);
  4393. else
  4394. vty_out (vty, " neighbor %s allowas-in %d%s", addr,
  4395. peer->allowas_in[afi][safi], VTY_NEWLINE);
  4396. }
  4397. /* Filter. */
  4398. bgp_config_write_filter (vty, peer, afi, safi);
  4399. /* atribute-unchanged. */
  4400. if ((CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)
  4401. || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)
  4402. || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED))
  4403. && ! peer->af_group[afi][safi])
  4404. {
  4405. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)
  4406. && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)
  4407. && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED))
  4408. vty_out (vty, " neighbor %s attribute-unchanged%s", addr, VTY_NEWLINE);
  4409. else
  4410. vty_out (vty, " neighbor %s attribute-unchanged%s%s%s%s", addr,
  4411. (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)) ?
  4412. " as-path" : "",
  4413. (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)) ?
  4414. " next-hop" : "",
  4415. (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED)) ?
  4416. " med" : "", VTY_NEWLINE);
  4417. }
  4418. }
  4419. /* Display "address-family" configuration header. */
  4420. void
  4421. bgp_config_write_family_header (struct vty *vty, afi_t afi, safi_t safi,
  4422. int *write)
  4423. {
  4424. if (*write)
  4425. return;
  4426. if (afi == AFI_IP && safi == SAFI_UNICAST)
  4427. return;
  4428. vty_out (vty, "!%s address-family ", VTY_NEWLINE);
  4429. if (afi == AFI_IP)
  4430. {
  4431. if (safi == SAFI_MULTICAST)
  4432. vty_out (vty, "ipv4 multicast");
  4433. else if (safi == SAFI_MPLS_VPN)
  4434. vty_out (vty, "vpnv4");
  4435. else if (safi == SAFI_ENCAP)
  4436. vty_out (vty, "encap");
  4437. }
  4438. else if (afi == AFI_IP6)
  4439. {
  4440. if (safi == SAFI_MPLS_VPN)
  4441. vty_out (vty, "vpnv6");
  4442. else if (safi == SAFI_ENCAP)
  4443. vty_out (vty, "encapv6");
  4444. else
  4445. {
  4446. vty_out (vty, "ipv6");
  4447. if (safi == SAFI_MULTICAST)
  4448. vty_out (vty, " multicast");
  4449. }
  4450. }
  4451. vty_out (vty, "%s", VTY_NEWLINE);
  4452. *write = 1;
  4453. }
  4454. /* Address family based peer configuration display. */
  4455. static int
  4456. bgp_config_write_family (struct vty *vty, struct bgp *bgp, afi_t afi,
  4457. safi_t safi)
  4458. {
  4459. int write = 0;
  4460. struct peer *peer;
  4461. struct peer_group *group;
  4462. struct listnode *node, *nnode;
  4463. bgp_config_write_network (vty, bgp, afi, safi, &write);
  4464. bgp_config_write_redistribute (vty, bgp, afi, safi, &write);
  4465. for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group))
  4466. {
  4467. if (group->conf->afc[afi][safi])
  4468. {
  4469. bgp_config_write_family_header (vty, afi, safi, &write);
  4470. bgp_config_write_peer (vty, bgp, group->conf, afi, safi);
  4471. }
  4472. }
  4473. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  4474. {
  4475. if (peer->afc[afi][safi])
  4476. {
  4477. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  4478. {
  4479. bgp_config_write_family_header (vty, afi, safi, &write);
  4480. bgp_config_write_peer (vty, bgp, peer, afi, safi);
  4481. }
  4482. }
  4483. }
  4484. bgp_config_write_maxpaths (vty, bgp, afi, safi, &write);
  4485. bgp_config_write_distance (vty, bgp, afi, safi, &write);
  4486. if (write)
  4487. vty_out (vty, " exit-address-family%s", VTY_NEWLINE);
  4488. return write;
  4489. }
  4490. int
  4491. bgp_config_write (struct vty *vty)
  4492. {
  4493. int write = 0;
  4494. struct bgp *bgp;
  4495. struct peer_group *group;
  4496. struct peer *peer;
  4497. struct listnode *node, *nnode;
  4498. struct listnode *mnode, *mnnode;
  4499. /* BGP Multiple instance. */
  4500. if (bgp_option_check (BGP_OPT_MULTIPLE_INSTANCE))
  4501. {
  4502. vty_out (vty, "bgp multiple-instance%s", VTY_NEWLINE);
  4503. write++;
  4504. }
  4505. /* BGP Config type. */
  4506. if (bgp_option_check (BGP_OPT_CONFIG_CISCO))
  4507. {
  4508. vty_out (vty, "bgp config-type cisco%s", VTY_NEWLINE);
  4509. write++;
  4510. }
  4511. /* BGP configuration. */
  4512. for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp))
  4513. {
  4514. if (write)
  4515. vty_out (vty, "!%s", VTY_NEWLINE);
  4516. /* Router bgp ASN */
  4517. vty_out (vty, "router bgp %u", bgp->as);
  4518. if (bgp_option_check (BGP_OPT_MULTIPLE_INSTANCE))
  4519. {
  4520. if (bgp->name)
  4521. vty_out (vty, " view %s", bgp->name);
  4522. }
  4523. vty_out (vty, "%s", VTY_NEWLINE);
  4524. /* No Synchronization */
  4525. if (bgp_option_check (BGP_OPT_CONFIG_CISCO))
  4526. vty_out (vty, " no synchronization%s", VTY_NEWLINE);
  4527. /* BGP fast-external-failover. */
  4528. if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
  4529. vty_out (vty, " no bgp fast-external-failover%s", VTY_NEWLINE);
  4530. /* BGP router ID. */
  4531. if (CHECK_FLAG (bgp->config, BGP_CONFIG_ROUTER_ID))
  4532. vty_out (vty, " bgp router-id %s%s", inet_ntoa (bgp->router_id),
  4533. VTY_NEWLINE);
  4534. /* BGP log-neighbor-changes. */
  4535. if (!bgp_flag_check (bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
  4536. vty_out (vty, " no bgp log-neighbor-changes%s", VTY_NEWLINE);
  4537. /* BGP configuration. */
  4538. if (bgp_flag_check (bgp, BGP_FLAG_ALWAYS_COMPARE_MED))
  4539. vty_out (vty, " bgp always-compare-med%s", VTY_NEWLINE);
  4540. /* BGP default ipv4-unicast. */
  4541. if (bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4))
  4542. vty_out (vty, " no bgp default ipv4-unicast%s", VTY_NEWLINE);
  4543. /* BGP default local-preference. */
  4544. if (bgp->default_local_pref != BGP_DEFAULT_LOCAL_PREF)
  4545. vty_out (vty, " bgp default local-preference %d%s",
  4546. bgp->default_local_pref, VTY_NEWLINE);
  4547. /* BGP client-to-client reflection. */
  4548. if (bgp_flag_check (bgp, BGP_FLAG_NO_CLIENT_TO_CLIENT))
  4549. vty_out (vty, " no bgp client-to-client reflection%s", VTY_NEWLINE);
  4550. /* BGP cluster ID. */
  4551. if (CHECK_FLAG (bgp->config, BGP_CONFIG_CLUSTER_ID))
  4552. vty_out (vty, " bgp cluster-id %s%s", inet_ntoa (bgp->cluster_id),
  4553. VTY_NEWLINE);
  4554. /* Confederation identifier*/
  4555. if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
  4556. vty_out (vty, " bgp confederation identifier %i%s", bgp->confed_id,
  4557. VTY_NEWLINE);
  4558. /* Confederation peer */
  4559. if (bgp->confed_peers_cnt > 0)
  4560. {
  4561. int i;
  4562. vty_out (vty, " bgp confederation peers");
  4563. for (i = 0; i < bgp->confed_peers_cnt; i++)
  4564. vty_out(vty, " %u", bgp->confed_peers[i]);
  4565. vty_out (vty, "%s", VTY_NEWLINE);
  4566. }
  4567. /* BGP enforce-first-as. */
  4568. if (bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS))
  4569. vty_out (vty, " bgp enforce-first-as%s", VTY_NEWLINE);
  4570. /* BGP deterministic-med. */
  4571. if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED))
  4572. vty_out (vty, " bgp deterministic-med%s", VTY_NEWLINE);
  4573. /* BGP graceful-restart. */
  4574. if (bgp->stalepath_time != BGP_DEFAULT_STALEPATH_TIME)
  4575. vty_out (vty, " bgp graceful-restart stalepath-time %d%s",
  4576. bgp->stalepath_time, VTY_NEWLINE);
  4577. if (bgp->restart_time != BGP_DEFAULT_RESTART_TIME)
  4578. vty_out (vty, " bgp graceful-restart restart-time %d%s",
  4579. bgp->restart_time, VTY_NEWLINE);
  4580. if (bgp_flag_check (bgp, BGP_FLAG_GRACEFUL_RESTART))
  4581. vty_out (vty, " bgp graceful-restart%s", VTY_NEWLINE);
  4582. /* BGP bestpath method. */
  4583. if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_IGNORE))
  4584. vty_out (vty, " bgp bestpath as-path ignore%s", VTY_NEWLINE);
  4585. if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_CONFED))
  4586. vty_out (vty, " bgp bestpath as-path confed%s", VTY_NEWLINE);
  4587. if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_MULTIPATH_RELAX)) {
  4588. vty_out (vty, " bgp bestpath as-path multipath-relax%s", VTY_NEWLINE);
  4589. }
  4590. if (bgp_flag_check (bgp, BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY)) {
  4591. vty_out (vty, " bgp route-reflector allow-outbound-policy%s",
  4592. VTY_NEWLINE);
  4593. }
  4594. if (bgp_flag_check (bgp, BGP_FLAG_COMPARE_ROUTER_ID))
  4595. vty_out (vty, " bgp bestpath compare-routerid%s", VTY_NEWLINE);
  4596. if (bgp_flag_check (bgp, BGP_FLAG_MED_CONFED)
  4597. || bgp_flag_check (bgp, BGP_FLAG_MED_MISSING_AS_WORST))
  4598. {
  4599. vty_out (vty, " bgp bestpath med");
  4600. if (bgp_flag_check (bgp, BGP_FLAG_MED_CONFED))
  4601. vty_out (vty, " confed");
  4602. if (bgp_flag_check (bgp, BGP_FLAG_MED_MISSING_AS_WORST))
  4603. vty_out (vty, " missing-as-worst");
  4604. vty_out (vty, "%s", VTY_NEWLINE);
  4605. }
  4606. /* BGP network import check. */
  4607. if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK))
  4608. vty_out (vty, " bgp network import-check%s", VTY_NEWLINE);
  4609. /* BGP flag dampening. */
  4610. if (CHECK_FLAG (bgp->af_flags[AFI_IP][SAFI_UNICAST],
  4611. BGP_CONFIG_DAMPENING))
  4612. bgp_config_write_damp (vty);
  4613. /* BGP static route configuration. */
  4614. bgp_config_write_network (vty, bgp, AFI_IP, SAFI_UNICAST, &write);
  4615. /* BGP redistribute configuration. */
  4616. bgp_config_write_redistribute (vty, bgp, AFI_IP, SAFI_UNICAST, &write);
  4617. /* BGP timers configuration. */
  4618. if (bgp->default_keepalive != BGP_DEFAULT_KEEPALIVE
  4619. && bgp->default_holdtime != BGP_DEFAULT_HOLDTIME)
  4620. vty_out (vty, " timers bgp %d %d%s", bgp->default_keepalive,
  4621. bgp->default_holdtime, VTY_NEWLINE);
  4622. /* peer-group */
  4623. for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group))
  4624. {
  4625. bgp_config_write_peer (vty, bgp, group->conf, AFI_IP, SAFI_UNICAST);
  4626. }
  4627. /* Normal neighbor configuration. */
  4628. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  4629. {
  4630. if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  4631. bgp_config_write_peer (vty, bgp, peer, AFI_IP, SAFI_UNICAST);
  4632. }
  4633. /* maximum-paths */
  4634. bgp_config_write_maxpaths (vty, bgp, AFI_IP, SAFI_UNICAST, &write);
  4635. /* Distance configuration. */
  4636. bgp_config_write_distance (vty, bgp, AFI_IP, SAFI_UNICAST, &write);
  4637. /* No auto-summary */
  4638. if (bgp_option_check (BGP_OPT_CONFIG_CISCO))
  4639. vty_out (vty, " no auto-summary%s", VTY_NEWLINE);
  4640. /* IPv4 multicast configuration. */
  4641. write += bgp_config_write_family (vty, bgp, AFI_IP, SAFI_MULTICAST);
  4642. /* IPv4 VPN configuration. */
  4643. write += bgp_config_write_family (vty, bgp, AFI_IP, SAFI_MPLS_VPN);
  4644. /* ENCAPv4 configuration. */
  4645. write += bgp_config_write_family (vty, bgp, AFI_IP, SAFI_ENCAP);
  4646. /* IPv6 unicast configuration. */
  4647. write += bgp_config_write_family (vty, bgp, AFI_IP6, SAFI_UNICAST);
  4648. /* IPv6 multicast configuration. */
  4649. write += bgp_config_write_family (vty, bgp, AFI_IP6, SAFI_MULTICAST);
  4650. /* IPv6 VPN configuration. */
  4651. write += bgp_config_write_family (vty, bgp, AFI_IP6, SAFI_MPLS_VPN);
  4652. /* ENCAPv6 configuration. */
  4653. write += bgp_config_write_family (vty, bgp, AFI_IP6, SAFI_ENCAP);
  4654. vty_out (vty, " exit%s", VTY_NEWLINE);
  4655. write++;
  4656. }
  4657. return write;
  4658. }
  4659. void
  4660. bgp_master_init (void)
  4661. {
  4662. memset (&bgp_master, 0, sizeof (struct bgp_master));
  4663. bm = &bgp_master;
  4664. bm->bgp = list_new ();
  4665. bm->listen_sockets = list_new ();
  4666. bm->port = BGP_PORT_DEFAULT;
  4667. bm->master = thread_master_create ();
  4668. bm->start_time = bgp_clock ();
  4669. }
  4670. void
  4671. bgp_init (void)
  4672. {
  4673. /* allocates some vital data structures used by peer commands in vty_init */
  4674. bgp_scan_init ();
  4675. /* Init zebra. */
  4676. bgp_zebra_init (bm->master);
  4677. /* BGP VTY commands installation. */
  4678. bgp_vty_init ();
  4679. /* BGP inits. */
  4680. bgp_attr_init ();
  4681. bgp_debug_init ();
  4682. bgp_dump_init ();
  4683. bgp_route_init ();
  4684. bgp_route_map_init ();
  4685. bgp_address_init ();
  4686. bgp_scan_vty_init();
  4687. bgp_mplsvpn_init ();
  4688. bgp_encap_init ();
  4689. /* Access list initialize. */
  4690. access_list_init ();
  4691. access_list_add_hook (peer_distribute_update);
  4692. access_list_delete_hook (peer_distribute_update);
  4693. /* Filter list initialize. */
  4694. bgp_filter_init ();
  4695. as_list_add_hook (peer_aslist_update);
  4696. as_list_delete_hook (peer_aslist_update);
  4697. /* Prefix list initialize.*/
  4698. prefix_list_init ();
  4699. prefix_list_add_hook (peer_prefix_list_update);
  4700. prefix_list_delete_hook (peer_prefix_list_update);
  4701. /* Community list initialize. */
  4702. bgp_clist = community_list_init ();
  4703. #ifdef HAVE_SNMP
  4704. bgp_snmp_init ();
  4705. #endif /* HAVE_SNMP */
  4706. }
  4707. void
  4708. bgp_terminate (void)
  4709. {
  4710. struct bgp *bgp;
  4711. struct peer *peer;
  4712. struct listnode *node, *nnode;
  4713. struct listnode *mnode, *mnnode;
  4714. for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp))
  4715. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  4716. if (peer->status == Established)
  4717. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  4718. BGP_NOTIFY_CEASE_PEER_UNCONFIG);
  4719. bgp_cleanup_routes ();
  4720. if (bm->process_main_queue)
  4721. {
  4722. work_queue_free (bm->process_main_queue);
  4723. bm->process_main_queue = NULL;
  4724. }
  4725. if (bm->process_rsclient_queue)
  4726. {
  4727. work_queue_free (bm->process_rsclient_queue);
  4728. bm->process_rsclient_queue = NULL;
  4729. }
  4730. }