bgpd.c 144 KB

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