bgpd.c 148 KB

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