bgpd.c 143 KB

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