bgpd.c 148 KB

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