bgpd.c 144 KB

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