bgpd.c 136 KB

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