]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgpd.c
ripd: Use correct usage of memcpy() when zeroing struct
[mirror_frr.git] / bgpd / bgpd.c
CommitLineData
718e3744 1/* BGP-4, BGP-4+ daemon program
896014f4
DL
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#include <zebra.h>
22
23#include "prefix.h"
24#include "thread.h"
25#include "buffer.h"
26#include "stream.h"
74ffbfe6 27#include "ringbuf.h"
718e3744 28#include "command.h"
29#include "sockunion.h"
e9a0c656 30#include "sockopt.h"
718e3744 31#include "network.h"
32#include "memory.h"
33#include "filter.h"
34#include "routemap.h"
718e3744 35#include "log.h"
36#include "plist.h"
37#include "linklist.h"
200df115 38#include "workqueue.h"
3f9c7369 39#include "queue.h"
88177fe3 40#include "zclient.h"
7f342629 41#include "bfd.h"
dd793e4a
DW
42#include "hash.h"
43#include "jhash.h"
587ff0fd 44#include "table.h"
4dcadbef 45#include "lib/json.h"
4ab46701 46#include "lib/sockopt.h"
0ca8b79f 47#include "frr_pthread.h"
92708db6 48#include "bitfield.h"
718e3744 49
50#include "bgpd/bgpd.h"
51#include "bgpd/bgp_table.h"
52#include "bgpd/bgp_aspath.h"
53#include "bgpd/bgp_route.h"
54#include "bgpd/bgp_dump.h"
55#include "bgpd/bgp_debug.h"
14454c9f 56#include "bgpd/bgp_errors.h"
718e3744 57#include "bgpd/bgp_community.h"
ed0e57e3 58#include "bgpd/bgp_community_alias.h"
7f7940e6 59#include "bgpd/bgp_conditional_adv.h"
718e3744 60#include "bgpd/bgp_attr.h"
61#include "bgpd/bgp_regex.h"
62#include "bgpd/bgp_clist.h"
63#include "bgpd/bgp_fsm.h"
64#include "bgpd/bgp_packet.h"
65#include "bgpd/bgp_zebra.h"
66#include "bgpd/bgp_open.h"
67#include "bgpd/bgp_filter.h"
68#include "bgpd/bgp_nexthop.h"
69#include "bgpd/bgp_damp.h"
70#include "bgpd/bgp_mplsvpn.h"
49e5a4a0 71#ifdef ENABLE_BGP_VNC
f8b6f499
LB
72#include "bgpd/rfapi/bgp_rfapi_cfg.h"
73#include "bgpd/rfapi/rfapi_backend.h"
65efcfce 74#endif
7ef5a232 75#include "bgpd/bgp_evpn.h"
718e3744 76#include "bgpd/bgp_advertise.h"
77#include "bgpd/bgp_network.h"
78#include "bgpd/bgp_vty.h"
165b5fff 79#include "bgpd/bgp_mpath.h"
fc9a856f 80#include "bgpd/bgp_nht.h"
3f9c7369 81#include "bgpd/bgp_updgrp.h"
c43ed2e4 82#include "bgpd/bgp_bfd.h"
4a1ab8e4 83#include "bgpd/bgp_memory.h"
784d3a42 84#include "bgpd/bgp_evpn_vty.h"
2d4ee774 85#include "bgpd/bgp_keepalives.h"
56257a44 86#include "bgpd/bgp_io.h"
41d6d607 87#include "bgpd/bgp_ecommunity.h"
7c40bf39 88#include "bgpd/bgp_flowspec.h"
955bfd98 89#include "bgpd/bgp_labelpool.h"
f3d32faa 90#include "bgpd/bgp_pbr.h"
dcc68b5e 91#include "bgpd/bgp_addpath.h"
85c8d83b 92#include "bgpd/bgp_evpn_private.h"
c44ab6f1 93#include "bgpd/bgp_evpn_mh.h"
48ecf8f5 94#include "bgpd/bgp_mac.h"
73d70fa6
DL
95
96DEFINE_MTYPE_STATIC(BGPD, PEER_TX_SHUTDOWN_MSG, "Peer shutdown message (TX)");
85c8d83b 97DEFINE_MTYPE_STATIC(BGPD, BGP_EVPN_INFO, "BGP EVPN instance information");
96244aca
DL
98DEFINE_QOBJ_TYPE(bgp_master);
99DEFINE_QOBJ_TYPE(bgp);
100DEFINE_QOBJ_TYPE(peer);
8451921b 101DEFINE_HOOK(bgp_inst_delete, (struct bgp *bgp), (bgp));
19df7279 102
718e3744 103/* BGP process wide configuration. */
104static struct bgp_master bgp_master;
105
106/* BGP process wide configuration pointer to export. */
107struct bgp_master *bm;
108
109/* BGP community-list. */
110struct community_list_handler *bgp_clist;
6b0655a2 111
37fe7731
DS
112unsigned int multipath_num = MULTIPATH_NUM;
113
c208c586
S
114/* Number of bgp instances configured for suppress fib config */
115unsigned int bgp_suppress_fib_count;
116
d62a17ae 117static void bgp_if_finish(struct bgp *bgp);
cc32742c 118static void peer_drop_dynamic_neighbor(struct peer *peer);
b33adb7c 119
ad4cbda1 120extern struct zclient *zclient;
1ff9a340 121
e5619c28
PG
122/* handle main socket creation or deletion */
123static int bgp_check_main_socket(bool create, struct bgp *bgp)
124{
125 static int bgp_server_main_created;
85e9cd9a
AMR
126 struct listnode *node;
127 char *address;
e5619c28 128
d8729f8c 129 if (create) {
e5619c28
PG
130 if (bgp_server_main_created)
131 return 0;
85e9cd9a
AMR
132 if (list_isempty(bm->addresses)) {
133 if (bgp_socket(bgp, bm->port, NULL) < 0)
134 return BGP_ERR_INVALID_VALUE;
135 } else {
136 for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
137 if (bgp_socket(bgp, bm->port, address) < 0)
138 return BGP_ERR_INVALID_VALUE;
139 }
e5619c28
PG
140 bgp_server_main_created = 1;
141 return 0;
142 }
143 if (!bgp_server_main_created)
144 return 0;
e5619c28
PG
145 bgp_close();
146 bgp_server_main_created = 0;
147 return 0;
148}
149
d62a17ae 150void bgp_session_reset(struct peer *peer)
1ff9a340 151{
d62a17ae 152 if (peer->doppelganger && (peer->doppelganger->status != Deleted)
153 && !(CHECK_FLAG(peer->doppelganger->flags, PEER_FLAG_CONFIG_NODE)))
154 peer_delete(peer->doppelganger);
1ff9a340 155
d62a17ae 156 BGP_EVENT_ADD(peer, BGP_Stop);
1ff9a340
DS
157}
158
159/*
160 * During session reset, we may delete the doppelganger peer, which would
161 * be the next node to the current node. If the session reset was invoked
162 * during walk of peer list, we would end up accessing the freed next
163 * node. This function moves the next node along.
164 */
d62a17ae 165static void bgp_session_reset_safe(struct peer *peer, struct listnode **nnode)
1ff9a340 166{
d62a17ae 167 struct listnode *n;
168 struct peer *npeer;
1ff9a340 169
d62a17ae 170 n = (nnode) ? *nnode : NULL;
171 npeer = (n) ? listgetdata(n) : NULL;
1ff9a340 172
d62a17ae 173 if (peer->doppelganger && (peer->doppelganger->status != Deleted)
174 && !(CHECK_FLAG(peer->doppelganger->flags,
175 PEER_FLAG_CONFIG_NODE))) {
176 if (peer->doppelganger == npeer)
177 /* nnode and *nnode are confirmed to be non-NULL here */
178 *nnode = (*nnode)->next;
179 peer_delete(peer->doppelganger);
180 }
1ff9a340 181
d62a17ae 182 BGP_EVENT_ADD(peer, BGP_Stop);
1ff9a340
DS
183}
184
718e3744 185/* BGP global flag manipulation. */
d62a17ae 186int bgp_option_set(int flag)
187{
188 switch (flag) {
189 case BGP_OPT_NO_FIB:
d62a17ae 190 case BGP_OPT_NO_LISTEN:
0b014ea6 191 case BGP_OPT_NO_ZEBRA:
d62a17ae 192 SET_FLAG(bm->options, flag);
193 break;
194 default:
195 return BGP_ERR_INVALID_FLAG;
196 }
197 return 0;
198}
199
200int bgp_option_unset(int flag)
201{
202 switch (flag) {
d62a17ae 203 /* Fall through. */
0b014ea6 204 case BGP_OPT_NO_ZEBRA:
d62a17ae 205 case BGP_OPT_NO_FIB:
d62a17ae 206 UNSET_FLAG(bm->options, flag);
207 break;
208 default:
209 return BGP_ERR_INVALID_FLAG;
210 }
211 return 0;
212}
213
214int bgp_option_check(int flag)
215{
216 return CHECK_FLAG(bm->options, flag);
718e3744 217}
6b0655a2 218
c163f297
DS
219/* set the bgp no-rib option during runtime and remove installed routes */
220void bgp_option_norib_set_runtime(void)
221{
222 struct bgp *bgp;
223 struct listnode *node;
224 afi_t afi;
225 safi_t safi;
226
227 if (bgp_option_check(BGP_OPT_NO_FIB))
228 return;
229
230 bgp_option_set(BGP_OPT_NO_FIB);
231
232 zlog_info("Disabled BGP route installation to RIB (Zebra)");
233
234 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
235 FOREACH_AFI_SAFI(afi, safi)
236 bgp_zebra_withdraw_table_all_subtypes(bgp, afi, safi);
237 }
238
239 zlog_info("All routes have been withdrawn from RIB (Zebra)");
240}
241
242/* unset the bgp no-rib option during runtime and announce routes to Zebra */
243void bgp_option_norib_unset_runtime(void)
244{
245 struct bgp *bgp;
246 struct listnode *node;
247 afi_t afi;
248 safi_t safi;
249
250 if (!bgp_option_check(BGP_OPT_NO_FIB))
251 return;
252
253 bgp_option_unset(BGP_OPT_NO_FIB);
254
255 zlog_info("Enabled BGP route installation to RIB (Zebra)");
256
257 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
258 FOREACH_AFI_SAFI(afi, safi)
259 bgp_zebra_announce_table_all_subtypes(bgp, afi, safi);
260 }
261
262 zlog_info("All routes have been installed in RIB (Zebra)");
263}
264
718e3744 265/* Internal function to set BGP structure configureation flag. */
d62a17ae 266static void bgp_config_set(struct bgp *bgp, int config)
718e3744 267{
d62a17ae 268 SET_FLAG(bgp->config, config);
718e3744 269}
270
d62a17ae 271static void bgp_config_unset(struct bgp *bgp, int config)
718e3744 272{
d62a17ae 273 UNSET_FLAG(bgp->config, config);
718e3744 274}
275
d62a17ae 276static int bgp_config_check(struct bgp *bgp, int config)
718e3744 277{
d62a17ae 278 return CHECK_FLAG(bgp->config, config);
718e3744 279}
6b0655a2 280
e65fe398
MS
281/* Set BGP router identifier; distinguish between explicit config and other
282 * cases.
283 */
284static int bgp_router_id_set(struct bgp *bgp, const struct in_addr *id,
285 bool is_config)
718e3744 286{
d62a17ae 287 struct peer *peer;
288 struct listnode *node, *nnode;
718e3744 289
d62a17ae 290 if (IPV4_ADDR_SAME(&bgp->router_id, id))
291 return 0;
718e3744 292
d62a17ae 293 /* EVPN uses router id in RD, withdraw them */
94c2f693 294 if (is_evpn_enabled())
2951a7a4 295 bgp_evpn_handle_router_id_update(bgp, true);
2d48ee25 296
2951a7a4 297 vpn_handle_router_id_update(bgp, true, is_config);
636f7608 298
d62a17ae 299 IPV4_ADDR_COPY(&bgp->router_id, id);
718e3744 300
d62a17ae 301 /* Set all peer's local identifier with this value. */
302 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
303 IPV4_ADDR_COPY(&peer->local_id, id);
718e3744 304
d62a17ae 305 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
306 peer->last_reset = PEER_DOWN_RID_CHANGE;
307 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
308 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
309 }
310 }
14c1a7bf 311
d62a17ae 312 /* EVPN uses router id in RD, update them */
94c2f693 313 if (is_evpn_enabled())
2951a7a4 314 bgp_evpn_handle_router_id_update(bgp, false);
2d48ee25 315
2951a7a4 316 vpn_handle_router_id_update(bgp, false, is_config);
636f7608 317
d62a17ae 318 return 0;
718e3744 319}
320
d62a17ae 321void bgp_router_id_zebra_bump(vrf_id_t vrf_id, const struct prefix *router_id)
0e6cb743 322{
d62a17ae 323 struct listnode *node, *nnode;
324 struct bgp *bgp;
36dc7588 325 struct in_addr *addr = NULL;
326
327 if (router_id != NULL)
328 addr = (struct in_addr *)&(router_id->u.prefix4);
0e6cb743 329
d62a17ae 330 if (vrf_id == VRF_DEFAULT) {
331 /* Router-id change for default VRF has to also update all
332 * views. */
333 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
334 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
335 continue;
0e6cb743 336
36dc7588 337 if (addr)
338 bgp->router_id_zebra = *addr;
339 else
340 addr = &bgp->router_id_zebra;
341
342 if (!bgp->router_id_static.s_addr) {
343 /* Router ID is updated if there are no active
344 * peer sessions
345 */
346 if (bgp->established_peers == 0) {
347 if (BGP_DEBUG(zebra, ZEBRA))
137147c6 348 zlog_debug(
23d0a753 349 "RID change : vrf %s(%u), RTR ID %pI4",
137147c6 350 bgp->name_pretty,
23d0a753 351 bgp->vrf_id, addr);
3c2a1ad1
DS
352 /*
353 * if old router-id was 0x0, set flag
354 * to use this new value
355 */
356 bgp_router_id_set(bgp, addr,
357 (bgp->router_id.s_addr
358 == INADDR_ANY)
359 ? true
360 : false);
36dc7588 361 }
362 }
d62a17ae 363 }
364 } else {
365 bgp = bgp_lookup_by_vrf_id(vrf_id);
366 if (bgp) {
36dc7588 367 if (addr)
368 bgp->router_id_zebra = *addr;
369 else
370 addr = &bgp->router_id_zebra;
371
372 if (!bgp->router_id_static.s_addr) {
373 /* Router ID is updated if there are no active
374 * peer sessions
375 */
376 if (bgp->established_peers == 0) {
377 if (BGP_DEBUG(zebra, ZEBRA))
137147c6 378 zlog_debug(
23d0a753 379 "RID change : vrf %s(%u), RTR ID %pI4",
137147c6 380 bgp->name_pretty,
23d0a753 381 bgp->vrf_id, addr);
3c2a1ad1
DS
382 /*
383 * if old router-id was 0x0, set flag
384 * to use this new value
385 */
386 bgp_router_id_set(bgp, addr,
387 (bgp->router_id.s_addr
388 == INADDR_ANY)
389 ? true
390 : false);
36dc7588 391 }
392 }
0e6cb743 393
d62a17ae 394 }
395 }
0e6cb743
DL
396}
397
3dc339cd 398void bgp_router_id_static_set(struct bgp *bgp, struct in_addr id)
0e6cb743 399{
d62a17ae 400 bgp->router_id_static = id;
975a328e
DA
401 bgp_router_id_set(bgp,
402 id.s_addr != INADDR_ANY ? &id : &bgp->router_id_zebra,
2951a7a4 403 true /* is config */);
0e6cb743
DL
404}
405
9acb67cb
DS
406void bm_wait_for_fib_set(bool set)
407{
408 bool send_msg = false;
409
410 if (bm->wait_for_fib == set)
411 return;
412
413 bm->wait_for_fib = set;
414 if (set) {
415 if (bgp_suppress_fib_count == 0)
416 send_msg = true;
417 bgp_suppress_fib_count++;
418 } else {
419 bgp_suppress_fib_count--;
420 if (bgp_suppress_fib_count == 0)
421 send_msg = true;
422 }
423
424 if (send_msg && zclient)
425 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST,
426 zclient, set);
427}
428
c208c586
S
429/* Set the suppress fib pending for the bgp configuration */
430void bgp_suppress_fib_pending_set(struct bgp *bgp, bool set)
431{
432 bool send_msg = false;
433
434 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
435 return;
436
437 if (set) {
438 SET_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING);
439 /* Send msg to zebra for the first instance of bgp enabled
440 * with suppress fib
441 */
442 if (bgp_suppress_fib_count == 0)
443 send_msg = true;
444 bgp_suppress_fib_count++;
445 } else {
446 UNSET_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING);
447 bgp_suppress_fib_count--;
448
449 /* Send msg to zebra if there are no instances enabled
450 * with suppress fib
451 */
452 if (bgp_suppress_fib_count == 0)
453 send_msg = true;
454 }
455 /* Send route notify request to RIB */
456 if (send_msg) {
457 if (BGP_DEBUG(zebra, ZEBRA))
458 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
459
460 if (zclient)
461 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST,
462 zclient, set);
463 }
464}
465
718e3744 466/* BGP's cluster-id control. */
d62a17ae 467int bgp_cluster_id_set(struct bgp *bgp, struct in_addr *cluster_id)
718e3744 468{
d62a17ae 469 struct peer *peer;
470 struct listnode *node, *nnode;
718e3744 471
1ca2fd11
IR
472 if (bgp_config_check(bgp, BGP_CONFIG_CLUSTER_ID)
473 && IPV4_ADDR_SAME(&bgp->cluster_id, cluster_id))
474 return 0;
475
d62a17ae 476 IPV4_ADDR_COPY(&bgp->cluster_id, cluster_id);
477 bgp_config_set(bgp, BGP_CONFIG_CLUSTER_ID);
718e3744 478
d62a17ae 479 /* Clear all IBGP peer. */
480 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
481 if (peer->sort != BGP_PEER_IBGP)
482 continue;
718e3744 483
d62a17ae 484 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
485 peer->last_reset = PEER_DOWN_CLID_CHANGE;
486 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
487 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
488 }
489 }
490 return 0;
718e3744 491}
492
d62a17ae 493int bgp_cluster_id_unset(struct bgp *bgp)
718e3744 494{
d62a17ae 495 struct peer *peer;
496 struct listnode *node, *nnode;
718e3744 497
d62a17ae 498 if (!bgp_config_check(bgp, BGP_CONFIG_CLUSTER_ID))
499 return 0;
718e3744 500
d62a17ae 501 bgp->cluster_id.s_addr = 0;
502 bgp_config_unset(bgp, BGP_CONFIG_CLUSTER_ID);
718e3744 503
d62a17ae 504 /* Clear all IBGP peer. */
505 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
506 if (peer->sort != BGP_PEER_IBGP)
507 continue;
718e3744 508
d62a17ae 509 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
510 peer->last_reset = PEER_DOWN_CLID_CHANGE;
511 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
512 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
513 }
514 }
515 return 0;
718e3744 516}
6b0655a2 517
65957886
SH
518/* time_t value that is monotonicly increasing
519 * and uneffected by adjustments to system clock
520 */
d62a17ae 521time_t bgp_clock(void)
65957886 522{
d62a17ae 523 struct timeval tv;
65957886 524
d62a17ae 525 monotime(&tv);
526 return tv.tv_sec;
65957886
SH
527}
528
718e3744 529/* BGP timer configuration. */
3dc339cd 530void bgp_timers_set(struct bgp *bgp, uint32_t keepalive, uint32_t holdtime,
6c537a18 531 uint32_t connect_retry, uint32_t delayopen)
718e3744 532{
d62a17ae 533 bgp->default_keepalive =
534 (keepalive < holdtime / 3 ? keepalive : holdtime / 3);
535 bgp->default_holdtime = holdtime;
5d5393b9 536 bgp->default_connect_retry = connect_retry;
6c537a18 537 bgp->default_delayopen = delayopen;
718e3744 538}
539
5d5393b9 540/* mostly for completeness - CLI uses its own defaults */
3dc339cd 541void bgp_timers_unset(struct bgp *bgp)
718e3744 542{
d62a17ae 543 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
544 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
5d5393b9 545 bgp->default_connect_retry = BGP_DEFAULT_CONNECT_RETRY;
6c537a18 546 bgp->default_delayopen = BGP_DEFAULT_DELAYOPEN;
718e3744 547}
6b0655a2 548
718e3744 549/* BGP confederation configuration. */
ff8a8a7a 550void bgp_confederation_id_set(struct bgp *bgp, as_t as)
d62a17ae 551{
552 struct peer *peer;
553 struct listnode *node, *nnode;
554 int already_confed;
555
556 if (as == 0)
ff8a8a7a 557 return;
d62a17ae 558
559 /* Remember - were we doing confederation before? */
560 already_confed = bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION);
561 bgp->confed_id = as;
562 bgp_config_set(bgp, BGP_CONFIG_CONFEDERATION);
563
564 /* If we were doing confederation already, this is just an external
565 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
566 were not doing confederation before, reset all EBGP sessions. */
567 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
3061acc2 568 enum bgp_peer_sort ptype = peer_sort(peer);
469cbb0f 569
d62a17ae 570 /* We're looking for peers who's AS is not local or part of our
571 confederation. */
572 if (already_confed) {
469cbb0f 573 if (ptype == BGP_PEER_EBGP) {
d62a17ae 574 peer->local_as = as;
575 if (BGP_IS_VALID_STATE_FOR_NOTIF(
576 peer->status)) {
577 peer->last_reset =
578 PEER_DOWN_CONFED_ID_CHANGE;
579 bgp_notify_send(
580 peer, BGP_NOTIFY_CEASE,
581 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
582 } else
583 bgp_session_reset_safe(peer, &nnode);
584 }
585 } else {
586 /* Not doign confederation before, so reset every
587 non-local
588 session */
469cbb0f 589 if (ptype != BGP_PEER_IBGP) {
d62a17ae 590 /* Reset the local_as to be our EBGP one */
469cbb0f 591 if (ptype == BGP_PEER_EBGP)
d62a17ae 592 peer->local_as = as;
593 if (BGP_IS_VALID_STATE_FOR_NOTIF(
594 peer->status)) {
595 peer->last_reset =
596 PEER_DOWN_CONFED_ID_CHANGE;
597 bgp_notify_send(
598 peer, BGP_NOTIFY_CEASE,
599 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
600 } else
601 bgp_session_reset_safe(peer, &nnode);
602 }
603 }
718e3744 604 }
ff8a8a7a 605 return;
718e3744 606}
607
d62a17ae 608int bgp_confederation_id_unset(struct bgp *bgp)
718e3744 609{
d62a17ae 610 struct peer *peer;
611 struct listnode *node, *nnode;
612
613 bgp->confed_id = 0;
614 bgp_config_unset(bgp, BGP_CONFIG_CONFEDERATION);
718e3744 615
d62a17ae 616 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
617 /* We're looking for peers who's AS is not local */
618 if (peer_sort(peer) != BGP_PEER_IBGP) {
619 peer->local_as = bgp->as;
620 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
621 peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE;
622 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
623 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
624 }
718e3744 625
d62a17ae 626 else
627 bgp_session_reset_safe(peer, &nnode);
628 }
629 }
630 return 0;
718e3744 631}
632
d62a17ae 633/* Is an AS part of the confed or not? */
3dc339cd 634bool bgp_confederation_peers_check(struct bgp *bgp, as_t as)
718e3744 635{
d62a17ae 636 int i;
718e3744 637
d62a17ae 638 if (!bgp)
3dc339cd 639 return false;
718e3744 640
d62a17ae 641 for (i = 0; i < bgp->confed_peers_cnt; i++)
642 if (bgp->confed_peers[i] == as)
3dc339cd 643 return true;
718e3744 644
3dc339cd 645 return false;
d62a17ae 646}
718e3744 647
d62a17ae 648/* Add an AS to the confederation set. */
649int bgp_confederation_peers_add(struct bgp *bgp, as_t as)
650{
651 struct peer *peer;
652 struct listnode *node, *nnode;
653
d62a17ae 654 if (bgp->as == as)
655 return BGP_ERR_INVALID_AS;
656
657 if (bgp_confederation_peers_check(bgp, as))
658 return -1;
659
0b04fa0e
DS
660 bgp->confed_peers =
661 XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
662 (bgp->confed_peers_cnt + 1) * sizeof(as_t));
d62a17ae 663
664 bgp->confed_peers[bgp->confed_peers_cnt] = as;
665 bgp->confed_peers_cnt++;
666
667 if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)) {
668 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
669 if (peer->as == as) {
b50a0610 670 (void)peer_sort(peer);
d62a17ae 671 peer->local_as = bgp->as;
672 if (BGP_IS_VALID_STATE_FOR_NOTIF(
673 peer->status)) {
674 peer->last_reset =
675 PEER_DOWN_CONFED_PEER_CHANGE;
676 bgp_notify_send(
677 peer, BGP_NOTIFY_CEASE,
678 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
679 } else
680 bgp_session_reset_safe(peer, &nnode);
681 }
682 }
683 }
684 return 0;
718e3744 685}
686
687/* Delete an AS from the confederation set. */
d62a17ae 688int bgp_confederation_peers_remove(struct bgp *bgp, as_t as)
689{
690 int i;
691 int j;
692 struct peer *peer;
693 struct listnode *node, *nnode;
694
695 if (!bgp)
696 return -1;
697
698 if (!bgp_confederation_peers_check(bgp, as))
699 return -1;
700
701 for (i = 0; i < bgp->confed_peers_cnt; i++)
702 if (bgp->confed_peers[i] == as)
703 for (j = i + 1; j < bgp->confed_peers_cnt; j++)
704 bgp->confed_peers[j - 1] = bgp->confed_peers[j];
705
706 bgp->confed_peers_cnt--;
707
708 if (bgp->confed_peers_cnt == 0) {
709 if (bgp->confed_peers)
710 XFREE(MTYPE_BGP_CONFED_LIST, bgp->confed_peers);
711 bgp->confed_peers = NULL;
712 } else
713 bgp->confed_peers =
714 XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
715 bgp->confed_peers_cnt * sizeof(as_t));
716
717 /* Now reset any peer who's remote AS has just been removed from the
718 CONFED */
719 if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)) {
720 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
721 if (peer->as == as) {
b50a0610 722 (void)peer_sort(peer);
d62a17ae 723 peer->local_as = bgp->confed_id;
724 if (BGP_IS_VALID_STATE_FOR_NOTIF(
725 peer->status)) {
726 peer->last_reset =
727 PEER_DOWN_CONFED_PEER_CHANGE;
728 bgp_notify_send(
729 peer, BGP_NOTIFY_CEASE,
730 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
731 } else
732 bgp_session_reset_safe(peer, &nnode);
733 }
734 }
718e3744 735 }
718e3744 736
d62a17ae 737 return 0;
718e3744 738}
6b0655a2 739
718e3744 740/* Local preference configuration. */
d7c0a89a 741int bgp_default_local_preference_set(struct bgp *bgp, uint32_t local_pref)
718e3744 742{
d62a17ae 743 if (!bgp)
744 return -1;
718e3744 745
d62a17ae 746 bgp->default_local_pref = local_pref;
718e3744 747
d62a17ae 748 return 0;
718e3744 749}
750
d62a17ae 751int bgp_default_local_preference_unset(struct bgp *bgp)
718e3744 752{
d62a17ae 753 if (!bgp)
754 return -1;
718e3744 755
d62a17ae 756 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
718e3744 757
d62a17ae 758 return 0;
718e3744 759}
6b0655a2 760
3f9c7369 761/* Local preference configuration. */
d7c0a89a 762int bgp_default_subgroup_pkt_queue_max_set(struct bgp *bgp, uint32_t queue_size)
3f9c7369 763{
d62a17ae 764 if (!bgp)
765 return -1;
3f9c7369 766
d62a17ae 767 bgp->default_subgroup_pkt_queue_max = queue_size;
3f9c7369 768
d62a17ae 769 return 0;
3f9c7369
DS
770}
771
d62a17ae 772int bgp_default_subgroup_pkt_queue_max_unset(struct bgp *bgp)
3f9c7369 773{
d62a17ae 774 if (!bgp)
775 return -1;
776 bgp->default_subgroup_pkt_queue_max =
777 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX;
3f9c7369 778
d62a17ae 779 return 0;
3f9c7369
DS
780}
781
f14e6fdb 782/* Listen limit configuration. */
d62a17ae 783int bgp_listen_limit_set(struct bgp *bgp, int listen_limit)
f14e6fdb 784{
d62a17ae 785 if (!bgp)
786 return -1;
f14e6fdb 787
d62a17ae 788 bgp->dynamic_neighbors_limit = listen_limit;
f14e6fdb 789
d62a17ae 790 return 0;
f14e6fdb
DS
791}
792
d62a17ae 793int bgp_listen_limit_unset(struct bgp *bgp)
f14e6fdb 794{
d62a17ae 795 if (!bgp)
796 return -1;
f14e6fdb 797
d62a17ae 798 bgp->dynamic_neighbors_limit = BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT;
f14e6fdb 799
d62a17ae 800 return 0;
f14e6fdb
DS
801}
802
5c525538
RW
803int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi, iana_safi_t pkt_safi,
804 afi_t *afi, safi_t *safi)
9cabb64b 805{
d62a17ae 806 /* Map from IANA values to internal values, return error if
807 * values are unrecognized.
808 */
809 *afi = afi_iana2int(pkt_afi);
810 *safi = safi_iana2int(pkt_safi);
811 if (*afi == AFI_MAX || *safi == SAFI_MAX)
812 return -1;
9cabb64b 813
d62a17ae 814 return 0;
9cabb64b 815}
816
d62a17ae 817int bgp_map_afi_safi_int2iana(afi_t afi, safi_t safi, iana_afi_t *pkt_afi,
5c525538 818 iana_safi_t *pkt_safi)
9cabb64b 819{
d62a17ae 820 /* Map from internal values to IANA values, return error if
821 * internal values are bad (unexpected).
822 */
823 if (afi == AFI_MAX || safi == SAFI_MAX)
824 return -1;
825 *pkt_afi = afi_int2iana(afi);
826 *pkt_safi = safi_int2iana(safi);
827 return 0;
9cabb64b 828}
829
d62a17ae 830struct peer_af *peer_af_create(struct peer *peer, afi_t afi, safi_t safi)
3f9c7369 831{
d62a17ae 832 struct peer_af *af;
833 int afid;
210ec2a0 834 struct bgp *bgp;
3f9c7369 835
d62a17ae 836 if (!peer)
837 return NULL;
3f9c7369 838
d62a17ae 839 afid = afindex(afi, safi);
840 if (afid >= BGP_AF_MAX)
841 return NULL;
3f9c7369 842
210ec2a0 843 bgp = peer->bgp;
d62a17ae 844 assert(peer->peer_af_array[afid] == NULL);
3f9c7369 845
d62a17ae 846 /* Allocate new peer af */
847 af = XCALLOC(MTYPE_BGP_PEER_AF, sizeof(struct peer_af));
c8560b44 848
d62a17ae 849 peer->peer_af_array[afid] = af;
850 af->afi = afi;
851 af->safi = safi;
852 af->afid = afid;
853 af->peer = peer;
210ec2a0 854 bgp->af_peer_count[afi][safi]++;
3f9c7369 855
d62a17ae 856 return af;
3f9c7369
DS
857}
858
d62a17ae 859struct peer_af *peer_af_find(struct peer *peer, afi_t afi, safi_t safi)
3f9c7369 860{
d62a17ae 861 int afid;
3f9c7369 862
d62a17ae 863 if (!peer)
864 return NULL;
3f9c7369 865
d62a17ae 866 afid = afindex(afi, safi);
867 if (afid >= BGP_AF_MAX)
868 return NULL;
3f9c7369 869
d62a17ae 870 return peer->peer_af_array[afid];
3f9c7369
DS
871}
872
d62a17ae 873int peer_af_delete(struct peer *peer, afi_t afi, safi_t safi)
3f9c7369 874{
d62a17ae 875 struct peer_af *af;
876 int afid;
210ec2a0 877 struct bgp *bgp;
3f9c7369 878
d62a17ae 879 if (!peer)
880 return -1;
3f9c7369 881
d62a17ae 882 afid = afindex(afi, safi);
883 if (afid >= BGP_AF_MAX)
884 return -1;
3f9c7369 885
d62a17ae 886 af = peer->peer_af_array[afid];
887 if (!af)
888 return -1;
3f9c7369 889
210ec2a0 890 bgp = peer->bgp;
46aeabed
LS
891 bgp_soft_reconfig_table_task_cancel(bgp, bgp->rib[afi][safi], peer);
892
d62a17ae 893 bgp_stop_announce_route_timer(af);
3f9c7369 894
d62a17ae 895 if (PAF_SUBGRP(af)) {
896 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
897 zlog_debug("u%" PRIu64 ":s%" PRIu64 " remove peer %s",
898 af->subgroup->update_group->id,
899 af->subgroup->id, peer->host);
900 }
3f9c7369 901
210ec2a0 902
d62a17ae 903 update_subgroup_remove_peer(af->subgroup, af);
3f9c7369 904
210ec2a0
AS
905 if (bgp->af_peer_count[afi][safi])
906 bgp->af_peer_count[afi][safi]--;
907
d62a17ae 908 peer->peer_af_array[afid] = NULL;
909 XFREE(MTYPE_BGP_PEER_AF, af);
910 return 0;
3f9c7369
DS
911}
912
eb821189 913/* Peer comparison function for sorting. */
d62a17ae 914int peer_cmp(struct peer *p1, struct peer *p2)
718e3744 915{
d62a17ae 916 if (p1->group && !p2->group)
917 return -1;
0aaa34db 918
d62a17ae 919 if (!p1->group && p2->group)
920 return 1;
0aaa34db 921
d62a17ae 922 if (p1->group == p2->group) {
923 if (p1->conf_if && !p2->conf_if)
924 return -1;
0aaa34db 925
d62a17ae 926 if (!p1->conf_if && p2->conf_if)
927 return 1;
0aaa34db 928
d62a17ae 929 if (p1->conf_if && p2->conf_if)
930 return if_cmp_name_func(p1->conf_if, p2->conf_if);
931 } else
932 return strcmp(p1->group->name, p2->group->name);
0aaa34db 933
d62a17ae 934 return sockunion_cmp(&p1->su, &p2->su);
718e3744 935}
936
d8b87afe 937static unsigned int peer_hash_key_make(const void *p)
dd793e4a 938{
d8b87afe 939 const struct peer *peer = p;
d62a17ae 940 return sockunion_hash(&peer->su);
dd793e4a
DW
941}
942
74df8d6d 943static bool peer_hash_same(const void *p1, const void *p2)
dd793e4a 944{
d62a17ae 945 const struct peer *peer1 = p1;
946 const struct peer *peer2 = p2;
947 return (sockunion_same(&peer1->su, &peer2->su)
948 && CHECK_FLAG(peer1->flags, PEER_FLAG_CONFIG_NODE)
949 == CHECK_FLAG(peer2->flags, PEER_FLAG_CONFIG_NODE));
dd793e4a
DW
950}
951
9fb964de
PM
952void peer_flag_inherit(struct peer *peer, uint32_t flag)
953{
954 bool group_val;
955
956 /* Skip if peer is not a peer-group member. */
957 if (!peer_group_active(peer))
958 return;
959
960 /* Unset override flag to signal inheritance from peer-group. */
961 UNSET_FLAG(peer->flags_override, flag);
962
963 /*
964 * Inherit flag state from peer-group. If the flag of the peer-group is
965 * not being inverted, the peer must inherit the inverse of the current
966 * peer-group flag state.
967 */
968 group_val = CHECK_FLAG(peer->group->conf->flags, flag);
969 if (!CHECK_FLAG(peer->group->conf->flags_invert, flag)
970 && CHECK_FLAG(peer->flags_invert, flag))
971 COND_FLAG(peer->flags, flag, !group_val);
972 else
973 COND_FLAG(peer->flags, flag, group_val);
974}
975
d7c0a89a 976int peer_af_flag_check(struct peer *peer, afi_t afi, safi_t safi, uint32_t flag)
718e3744 977{
d62a17ae 978 return CHECK_FLAG(peer->af_flags[afi][safi], flag);
718e3744 979}
980
598ce6bd
PM
981void peer_af_flag_inherit(struct peer *peer, afi_t afi, safi_t safi,
982 uint32_t flag)
88b8ed8d 983{
527de3dc
PM
984 bool group_val;
985
598ce6bd
PM
986 /* Skip if peer is not a peer-group member. */
987 if (!peer_group_active(peer))
988 return;
88b8ed8d 989
598ce6bd
PM
990 /* Unset override flag to signal inheritance from peer-group. */
991 UNSET_FLAG(peer->af_flags_override[afi][safi], flag);
88b8ed8d 992
527de3dc
PM
993 /*
994 * Inherit flag state from peer-group. If the flag of the peer-group is
995 * not being inverted, the peer must inherit the inverse of the current
996 * peer-group flag state.
997 */
998 group_val = CHECK_FLAG(peer->group->conf->af_flags[afi][safi], flag);
999 if (!CHECK_FLAG(peer->group->conf->af_flags_invert[afi][safi], flag)
1000 && CHECK_FLAG(peer->af_flags_invert[afi][safi], flag))
1001 COND_FLAG(peer->af_flags[afi][safi], flag, !group_val);
598ce6bd 1002 else
527de3dc 1003 COND_FLAG(peer->af_flags[afi][safi], flag, group_val);
598ce6bd 1004}
88b8ed8d 1005
6d85b15b 1006/* Check peer's AS number and determines if this peer is IBGP or EBGP */
3061acc2 1007static inline enum bgp_peer_sort peer_calc_sort(struct peer *peer)
718e3744 1008{
d62a17ae 1009 struct bgp *bgp;
718e3744 1010
d62a17ae 1011 bgp = peer->bgp;
718e3744 1012
d62a17ae 1013 /* Peer-group */
1014 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
1015 if (peer->as_type == AS_INTERNAL)
1016 return BGP_PEER_IBGP;
66b199b2 1017
d62a17ae 1018 else if (peer->as_type == AS_EXTERNAL)
1019 return BGP_PEER_EBGP;
66b199b2 1020
b575a12c
A
1021 else if (peer->as_type == AS_SPECIFIED && peer->as) {
1022 assert(bgp);
d62a17ae 1023 return (bgp->as == peer->as ? BGP_PEER_IBGP
1024 : BGP_PEER_EBGP);
b575a12c 1025 }
66b199b2 1026
d62a17ae 1027 else {
1028 struct peer *peer1;
d4f8b537 1029
1030 assert(peer->group);
d62a17ae 1031 peer1 = listnode_head(peer->group->peer);
1032
1033 if (peer1)
1034 return peer1->sort;
1035 }
1036 return BGP_PEER_INTERNAL;
1037 }
1038
1039 /* Normal peer */
1040 if (bgp && CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
1041 if (peer->local_as == 0)
1042 return BGP_PEER_INTERNAL;
1043
1044 if (peer->local_as == peer->as) {
1045 if (bgp->as == bgp->confed_id) {
1046 if (peer->local_as == bgp->as)
1047 return BGP_PEER_IBGP;
1048 else
1049 return BGP_PEER_EBGP;
1050 } else {
1051 if (peer->local_as == bgp->confed_id)
1052 return BGP_PEER_EBGP;
1053 else
1054 return BGP_PEER_IBGP;
1055 }
1056 }
1057
1058 if (bgp_confederation_peers_check(bgp, peer->as))
1059 return BGP_PEER_CONFED;
1060
1061 return BGP_PEER_EBGP;
1062 } else {
faa16034
DS
1063 if (peer->as_type == AS_UNSPECIFIED) {
1064 /* check if in peer-group with AS information */
1065 if (peer->group
1066 && (peer->group->conf->as_type != AS_UNSPECIFIED)) {
1067 if (peer->group->conf->as_type
1068 == AS_SPECIFIED) {
1069 if (peer->local_as
1070 == peer->group->conf->as)
1071 return BGP_PEER_IBGP;
1072 else
1073 return BGP_PEER_EBGP;
1074 } else if (peer->group->conf->as_type
1075 == AS_INTERNAL)
1076 return BGP_PEER_IBGP;
1077 else
1078 return BGP_PEER_EBGP;
1079 }
1080 /* no AS information anywhere, let caller know */
1081 return BGP_PEER_UNSPECIFIED;
1082 } else if (peer->as_type != AS_SPECIFIED)
d62a17ae 1083 return (peer->as_type == AS_INTERNAL ? BGP_PEER_IBGP
1084 : BGP_PEER_EBGP);
1085
1086 return (peer->local_as == 0
1087 ? BGP_PEER_INTERNAL
1088 : peer->local_as == peer->as ? BGP_PEER_IBGP
1089 : BGP_PEER_EBGP);
1090 }
718e3744 1091}
1092
6d85b15b 1093/* Calculate and cache the peer "sort" */
3061acc2 1094enum bgp_peer_sort peer_sort(struct peer *peer)
d62a17ae 1095{
1096 peer->sort = peer_calc_sort(peer);
1097 return peer->sort;
1098}
1099
3061acc2 1100enum bgp_peer_sort peer_sort_lookup(struct peer *peer)
bf0d28dc
DS
1101{
1102 return peer->sort;
1103}
1104
d62a17ae 1105static void peer_free(struct peer *peer)
1106{
dcc68b5e
MS
1107 afi_t afi;
1108 safi_t safi;
1109
d62a17ae 1110 assert(peer->status == Deleted);
1111
1112 QOBJ_UNREG(peer);
1113
1114 /* this /ought/ to have been done already through bgp_stop earlier,
1115 * but just to be sure..
1116 */
1117 bgp_timer_set(peer);
424ab01d
QY
1118 bgp_reads_off(peer);
1119 bgp_writes_off(peer);
1120 assert(!peer->t_write);
1121 assert(!peer->t_read);
d62a17ae 1122 BGP_EVENT_FLUSH(peer);
1123
424ab01d
QY
1124 pthread_mutex_destroy(&peer->io_mtx);
1125
d62a17ae 1126 /* Free connected nexthop, if present */
1127 if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)
1128 && !peer_dynamic_neighbor(peer))
1129 bgp_delete_connected_nexthop(family2afi(peer->su.sa.sa_family),
1130 peer);
1131
3d1c148b
DS
1132 FOREACH_AFI_SAFI (afi, safi) {
1133 if (peer->filter[afi][safi].advmap.aname)
1134 XFREE(MTYPE_BGP_FILTER_NAME,
1135 peer->filter[afi][safi].advmap.aname);
1136 if (peer->filter[afi][safi].advmap.cname)
1137 XFREE(MTYPE_BGP_FILTER_NAME,
1138 peer->filter[afi][safi].advmap.cname);
1139 }
1140
d62a17ae 1141 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
1142
e1b36e13
QY
1143 XFREE(MTYPE_PEER_DESC, peer->desc);
1144 XFREE(MTYPE_BGP_PEER_HOST, peer->host);
1145 XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
1146 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
d62a17ae 1147
1148 /* Update source configuration. */
1149 if (peer->update_source) {
1150 sockunion_free(peer->update_source);
1151 peer->update_source = NULL;
1152 }
1153
e1b36e13 1154 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
d62a17ae 1155
eea685b6 1156 XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data);
d62a17ae 1157 memset(&peer->notify, 0, sizeof(struct bgp_notify));
1158
e208c8f9
DS
1159 if (peer->clear_node_queue)
1160 work_queue_free_and_null(&peer->clear_node_queue);
d62a17ae 1161
1162 bgp_sync_delete(peer);
1163
e1b36e13 1164 XFREE(MTYPE_PEER_CONF_IF, peer->conf_if);
d62a17ae 1165
21bfce98
RZ
1166 /* Remove BFD configuration. */
1167 if (peer->bfd_config)
1168 bgp_peer_remove_bfd_config(peer);
d62a17ae 1169
3d1c148b
DS
1170 FOREACH_AFI_SAFI (afi, safi)
1171 bgp_addpath_set_peer_type(peer, afi, safi, BGP_ADDPATH_NONE);
dcc68b5e 1172
d62a17ae 1173 bgp_unlock(peer->bgp);
1174
1175 memset(peer, 0, sizeof(struct peer));
1176
1177 XFREE(MTYPE_BGP_PEER, peer);
1178}
1179
200df115 1180/* increase reference count on a struct peer */
d62a17ae 1181struct peer *peer_lock_with_caller(const char *name, struct peer *peer)
200df115 1182{
d62a17ae 1183 assert(peer && (peer->lock >= 0));
28066f4b 1184
d62a17ae 1185 peer->lock++;
1186
1187 return peer;
200df115 1188}
1189
1190/* decrease reference count on a struct peer
1191 * struct peer is freed and NULL returned if last reference
1192 */
d62a17ae 1193struct peer *peer_unlock_with_caller(const char *name, struct peer *peer)
200df115 1194{
d62a17ae 1195 assert(peer && (peer->lock > 0));
28066f4b 1196
d62a17ae 1197 peer->lock--;
1198
1199 if (peer->lock == 0) {
1200 peer_free(peer);
1201 return NULL;
1202 }
1203
1204 return peer;
200df115 1205}
794b37d5 1206/* BGP GR changes */
1207
1208int bgp_global_gr_init(struct bgp *bgp)
1209{
1210 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
1211 zlog_debug("%s called ..", __func__);
1212
2ba1fe69 1213 int local_GLOBAL_GR_FSM[BGP_GLOBAL_GR_MODE][BGP_GLOBAL_GR_EVENT_CMD] = {
794b37d5 1214 /* GLOBAL_HELPER Mode */
1215 {
1216 /*Event -> */
1217 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1218 GLOBAL_GR, GLOBAL_INVALID,
1219 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1220 GLOBAL_DISABLE, GLOBAL_INVALID
1221 },
1222 /* GLOBAL_GR Mode */
1223 {
1224 /*Event -> */
1225 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
62403772 1226 GLOBAL_GR, GLOBAL_HELPER,
794b37d5 1227 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1228 GLOBAL_DISABLE, GLOBAL_INVALID
1229 },
1230 /* GLOBAL_DISABLE Mode */
1231 {
1232 /*Event -> */
1233 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1234 GLOBAL_GR, GLOBAL_INVALID,
1235 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1236 GLOBAL_INVALID, GLOBAL_HELPER
1237 },
1238 /* GLOBAL_INVALID Mode */
1239 {
1240 /*Event -> */
1241 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1242 GLOBAL_INVALID, GLOBAL_INVALID,
1243 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1244 GLOBAL_INVALID, GLOBAL_INVALID
1245 }
1246 };
1247 memcpy(bgp->GLOBAL_GR_FSM, local_GLOBAL_GR_FSM,
1248 sizeof(local_GLOBAL_GR_FSM));
1249
1250 bgp->global_gr_present_state = GLOBAL_HELPER;
dc95985f 1251 bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
794b37d5 1252
1253 return BGP_GR_SUCCESS;
1254}
1255
1256int bgp_peer_gr_init(struct peer *peer)
1257{
1258 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
1259 zlog_debug("%s called ..", __func__);
1260
2ba1fe69 1261 struct bgp_peer_gr local_Peer_GR_FSM[BGP_PEER_GR_MODE]
1262 [BGP_PEER_GR_EVENT_CMD] = {
794b37d5 1263 {
1264 /* PEER_HELPER Mode */
1265 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1266 { PEER_GR, bgp_peer_gr_action }, {PEER_INVALID, NULL },
2ba1fe69 1267 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
794b37d5 1268 {PEER_DISABLE, bgp_peer_gr_action }, {PEER_INVALID, NULL },
1269 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1270 { PEER_INVALID, NULL }, {PEER_GLOBAL_INHERIT,
1271 bgp_peer_gr_action }
1272 },
1273 {
1274 /* PEER_GR Mode */
1275 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1276 { PEER_INVALID, NULL }, { PEER_GLOBAL_INHERIT,
1277 bgp_peer_gr_action },
2ba1fe69 1278 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
794b37d5 1279 {PEER_DISABLE, bgp_peer_gr_action }, { PEER_INVALID, NULL },
1280 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1281 { PEER_HELPER, bgp_peer_gr_action }, { PEER_INVALID, NULL }
1282 },
1283 {
1284 /* PEER_DISABLE Mode */
1285 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1286 { PEER_GR, bgp_peer_gr_action }, { PEER_INVALID, NULL },
2ba1fe69 1287 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
794b37d5 1288 { PEER_INVALID, NULL }, { PEER_GLOBAL_INHERIT,
1289 bgp_peer_gr_action },
1290 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1291 { PEER_HELPER, bgp_peer_gr_action }, { PEER_INVALID, NULL }
1292 },
1293 {
1294 /* PEER_INVALID Mode */
1295 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1296 { PEER_INVALID, NULL }, { PEER_INVALID, NULL },
2ba1fe69 1297 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
794b37d5 1298 { PEER_INVALID, NULL }, { PEER_INVALID, NULL },
1299 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1300 { PEER_INVALID, NULL }, { PEER_INVALID, NULL },
1301 },
1302 {
1303 /* PEER_GLOBAL_INHERIT Mode */
1304 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1305 { PEER_GR, bgp_peer_gr_action }, { PEER_INVALID, NULL },
2ba1fe69 1306 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
794b37d5 1307 { PEER_DISABLE, bgp_peer_gr_action}, { PEER_INVALID, NULL },
1308 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1309 { PEER_HELPER, bgp_peer_gr_action }, { PEER_INVALID, NULL }
1310 }
1311 };
1312 memcpy(&peer->PEER_GR_FSM, local_Peer_GR_FSM,
1313 sizeof(local_Peer_GR_FSM));
1314 peer->peer_gr_present_state = PEER_GLOBAL_INHERIT;
1315 bgp_peer_move_to_gr_mode(peer, PEER_GLOBAL_INHERIT);
1316
1317 return BGP_GR_SUCCESS;
1318}
1ff9a340 1319
92a9e6f2
HS
1320static void bgp_srv6_init(struct bgp *bgp)
1321{
1322 bgp->srv6_enabled = false;
1323 memset(bgp->srv6_locator_name, 0, sizeof(bgp->srv6_locator_name));
1324 bgp->srv6_locator_chunks = list_new();
1325 bgp->srv6_functions = list_new();
1326}
1327
1328static void bgp_srv6_cleanup(struct bgp *bgp)
1329{
124f7236
HS
1330 if (bgp->srv6_locator_chunks)
1331 list_delete(&bgp->srv6_locator_chunks);
1332 if (bgp->srv6_functions)
1333 list_delete(&bgp->srv6_functions);
92a9e6f2
HS
1334}
1335
200df115 1336/* Allocate new peer object, implicitely locked. */
d62a17ae 1337struct peer *peer_new(struct bgp *bgp)
1338{
1339 afi_t afi;
1340 safi_t safi;
1341 struct peer *peer;
1342 struct servent *sp;
1343
1344 /* bgp argument is absolutely required */
1345 assert(bgp);
d62a17ae 1346
1347 /* Allocate new peer. */
1348 peer = XCALLOC(MTYPE_BGP_PEER, sizeof(struct peer));
1349
1350 /* Set default value. */
1351 peer->fd = -1;
1352 peer->v_start = BGP_INIT_START_TIMER;
5d5393b9 1353 peer->v_connect = bgp->default_connect_retry;
d62a17ae 1354 peer->status = Idle;
1355 peer->ostatus = Idle;
1356 peer->cur_event = peer->last_event = peer->last_major_event = 0;
92375c91 1357 peer->bgp = bgp_lock(bgp);
d62a17ae 1358 peer = peer_lock(peer); /* initial reference */
d62a17ae 1359 peer->password = NULL;
0db06e37 1360 peer->max_packet_size = BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE;
d62a17ae 1361
27c05d4d 1362 /* Set default flags. */
05c7a1cc 1363 FOREACH_AFI_SAFI (afi, safi) {
d8a9922d
DS
1364 SET_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY);
1365 SET_FLAG(peer->af_flags[afi][safi],
1366 PEER_FLAG_SEND_EXT_COMMUNITY);
1367 SET_FLAG(peer->af_flags[afi][safi],
1368 PEER_FLAG_SEND_LARGE_COMMUNITY);
1369
1370 SET_FLAG(peer->af_flags_invert[afi][safi],
1371 PEER_FLAG_SEND_COMMUNITY);
1372 SET_FLAG(peer->af_flags_invert[afi][safi],
1373 PEER_FLAG_SEND_EXT_COMMUNITY);
1374 SET_FLAG(peer->af_flags_invert[afi][safi],
1375 PEER_FLAG_SEND_LARGE_COMMUNITY);
dcc68b5e 1376 peer->addpath_type[afi][safi] = BGP_ADDPATH_NONE;
05c7a1cc 1377 }
637315fc 1378
bd23c840
PR
1379 /* set nexthop-unchanged for l2vpn evpn by default */
1380 SET_FLAG(peer->af_flags[AFI_L2VPN][SAFI_EVPN],
1381 PEER_FLAG_NEXTHOP_UNCHANGED);
1382
d62a17ae 1383 SET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
718e3744 1384
794b37d5 1385 /* Initialize per peer bgp GR FSM */
1386 bgp_peer_gr_init(peer);
1387
d62a17ae 1388 /* Create buffers. */
424ab01d 1389 peer->ibuf = stream_fifo_new();
d62a17ae 1390 peer->obuf = stream_fifo_new();
424ab01d 1391 pthread_mutex_init(&peer->io_mtx, NULL);
0a91ff55 1392
424ab01d 1393 /* We use a larger buffer for peer->obuf_work in the event that:
d62a17ae 1394 * - We RX a BGP_UPDATE where the attributes alone are just
556beacf 1395 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
d62a17ae 1396 * - The user configures an outbound route-map that does many as-path
09f6d019
QY
1397 * prepends or adds many communities. At most they can have
1398 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1399 * large they can make the attributes.
d62a17ae 1400 *
1401 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
09f6d019
QY
1402 * bounds checking for every single attribute as we construct an
1403 * UPDATE.
d62a17ae 1404 */
556beacf
QY
1405 peer->obuf_work =
1406 stream_new(BGP_MAX_PACKET_SIZE + BGP_MAX_PACKET_SIZE_OVERFLOW);
1407 peer->ibuf_work =
1408 ringbuf_new(BGP_MAX_PACKET_SIZE * BGP_READ_PACKET_MAX);
74ffbfe6 1409
556beacf 1410 peer->scratch = stream_new(BGP_MAX_PACKET_SIZE);
718e3744 1411
d62a17ae 1412 bgp_sync_init(peer);
718e3744 1413
d62a17ae 1414 /* Get service port number. */
1415 sp = getservbyname("bgp", "tcp");
1416 peer->port = (sp == NULL) ? BGP_PORT_DEFAULT : ntohs(sp->s_port);
718e3744 1417
d62a17ae 1418 QOBJ_REG(peer, peer);
1419 return peer;
718e3744 1420}
1421
1ff9a340
DS
1422/*
1423 * This function is invoked when a duplicate peer structure associated with
1424 * a neighbor is being deleted. If this about-to-be-deleted structure is
1425 * the one with all the config, then we have to copy over the info.
1426 */
d62a17ae 1427void peer_xfer_config(struct peer *peer_dst, struct peer *peer_src)
1428{
1429 struct peer_af *paf;
1430 afi_t afi;
1431 safi_t safi;
1432 int afidx;
1433
1434 assert(peer_src);
1435 assert(peer_dst);
1436
1437 /* The following function is used by both peer group config copy to
1438 * individual peer and when we transfer config
1439 */
1440 if (peer_src->change_local_as)
1441 peer_dst->change_local_as = peer_src->change_local_as;
1442
1443 /* peer flags apply */
1444 peer_dst->flags = peer_src->flags;
d62a17ae 1445
d7b3cda6 1446 peer_dst->peer_gr_present_state = peer_src->peer_gr_present_state;
1447 peer_dst->peer_gr_new_status_flag = peer_src->peer_gr_new_status_flag;
1448
d62a17ae 1449 peer_dst->local_as = peer_src->local_as;
d62a17ae 1450 peer_dst->port = peer_src->port;
4ab46701
AR
1451 /* copy tcp_mss value */
1452 peer_dst->tcp_mss = peer_src->tcp_mss;
82417ac2 1453 (void)peer_sort(peer_dst);
d62a17ae 1454 peer_dst->rmap_type = peer_src->rmap_type;
1455
0db06e37
DA
1456 peer_dst->max_packet_size = peer_src->max_packet_size;
1457
d62a17ae 1458 /* Timers */
1459 peer_dst->holdtime = peer_src->holdtime;
1460 peer_dst->keepalive = peer_src->keepalive;
1461 peer_dst->connect = peer_src->connect;
6c537a18 1462 peer_dst->delayopen = peer_src->delayopen;
d62a17ae 1463 peer_dst->v_holdtime = peer_src->v_holdtime;
1464 peer_dst->v_keepalive = peer_src->v_keepalive;
1465 peer_dst->routeadv = peer_src->routeadv;
1466 peer_dst->v_routeadv = peer_src->v_routeadv;
6c537a18 1467 peer_dst->v_delayopen = peer_src->v_delayopen;
d62a17ae 1468
1469 /* password apply */
1470 if (peer_src->password && !peer_dst->password)
1471 peer_dst->password =
1472 XSTRDUP(MTYPE_PEER_PASSWORD, peer_src->password);
1473
05c7a1cc
QY
1474 FOREACH_AFI_SAFI (afi, safi) {
1475 peer_dst->afc[afi][safi] = peer_src->afc[afi][safi];
1476 peer_dst->af_flags[afi][safi] = peer_src->af_flags[afi][safi];
1477 peer_dst->allowas_in[afi][safi] =
1478 peer_src->allowas_in[afi][safi];
1479 peer_dst->weight[afi][safi] = peer_src->weight[afi][safi];
dcc68b5e
MS
1480 peer_dst->addpath_type[afi][safi] =
1481 peer_src->addpath_type[afi][safi];
05c7a1cc 1482 }
d62a17ae 1483
1484 for (afidx = BGP_AF_START; afidx < BGP_AF_MAX; afidx++) {
1485 paf = peer_src->peer_af_array[afidx];
e84c59af
DA
1486 if (paf != NULL) {
1487 if (!peer_af_find(peer_dst, paf->afi, paf->safi))
1488 peer_af_create(peer_dst, paf->afi, paf->safi);
1489 }
d62a17ae 1490 }
1491
1492 /* update-source apply */
1493 if (peer_src->update_source) {
1494 if (peer_dst->update_source)
1495 sockunion_free(peer_dst->update_source);
e1b36e13 1496 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer_dst->update_if);
d62a17ae 1497 peer_dst->update_source =
1498 sockunion_dup(peer_src->update_source);
1499 } else if (peer_src->update_if) {
0a22ddfb 1500 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer_dst->update_if);
d62a17ae 1501 if (peer_dst->update_source) {
1502 sockunion_free(peer_dst->update_source);
1503 peer_dst->update_source = NULL;
1504 }
1505 peer_dst->update_if =
1506 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, peer_src->update_if);
1507 }
1508
1509 if (peer_src->ifname) {
0a22ddfb 1510 XFREE(MTYPE_BGP_PEER_IFNAME, peer_dst->ifname);
d62a17ae 1511
1512 peer_dst->ifname =
1513 XSTRDUP(MTYPE_BGP_PEER_IFNAME, peer_src->ifname);
1514 }
1515}
1516
1517static int bgp_peer_conf_if_to_su_update_v4(struct peer *peer,
1518 struct interface *ifp)
1519{
1520 struct connected *ifc;
1521 struct prefix p;
d7c0a89a 1522 uint32_t addr;
d62a17ae 1523 struct listnode *node;
1524
1525 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1526 * IPv4 address of the other end.
1527 */
1528 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
1529 if (ifc->address && (ifc->address->family == AF_INET)) {
b08dcc3f 1530 prefix_copy(&p, CONNECTED_PREFIX(ifc));
d62a17ae 1531 if (p.prefixlen == 30) {
1532 peer->su.sa.sa_family = AF_INET;
1533 addr = ntohl(p.u.prefix4.s_addr);
1534 if (addr % 4 == 1)
1535 peer->su.sin.sin_addr.s_addr =
1536 htonl(addr + 1);
1537 else if (addr % 4 == 2)
1538 peer->su.sin.sin_addr.s_addr =
1539 htonl(addr - 1);
8ffedcea 1540#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 1541 peer->su.sin.sin_len =
1542 sizeof(struct sockaddr_in);
8ffedcea 1543#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 1544 return 1;
1545 } else if (p.prefixlen == 31) {
1546 peer->su.sa.sa_family = AF_INET;
1547 addr = ntohl(p.u.prefix4.s_addr);
1548 if (addr % 2 == 0)
1549 peer->su.sin.sin_addr.s_addr =
1550 htonl(addr + 1);
1551 else
1552 peer->su.sin.sin_addr.s_addr =
1553 htonl(addr - 1);
8ffedcea 1554#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 1555 peer->su.sin.sin_len =
1556 sizeof(struct sockaddr_in);
8ffedcea 1557#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 1558 return 1;
1559 } else if (bgp_debug_neighbor_events(peer))
1560 zlog_debug(
1561 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1562 peer->conf_if);
1563 }
1564 }
1565
1566 return 0;
1567}
1568
3dc339cd
DA
1569static bool bgp_peer_conf_if_to_su_update_v6(struct peer *peer,
1570 struct interface *ifp)
d62a17ae 1571{
1572 struct nbr_connected *ifc_nbr;
1573
1574 /* Have we learnt the peer's IPv6 link-local address? */
1575 if (ifp->nbr_connected
1576 && (ifc_nbr = listnode_head(ifp->nbr_connected))) {
1577 peer->su.sa.sa_family = AF_INET6;
1578 memcpy(&peer->su.sin6.sin6_addr, &ifc_nbr->address->u.prefix,
1579 sizeof(struct in6_addr));
a80beece 1580#ifdef SIN6_LEN
d62a17ae 1581 peer->su.sin6.sin6_len = sizeof(struct sockaddr_in6);
a80beece 1582#endif
d62a17ae 1583 peer->su.sin6.sin6_scope_id = ifp->ifindex;
3dc339cd 1584 return true;
d62a17ae 1585 }
8ffedcea 1586
3dc339cd 1587 return false;
89ca90fa 1588}
1589
1590/*
1591 * Set or reset the peer address socketunion structure based on the
1592 * learnt/derived peer address. If the address has changed, update the
1593 * password on the listen socket, if needed.
1594 */
d62a17ae 1595void bgp_peer_conf_if_to_su_update(struct peer *peer)
1596{
1597 struct interface *ifp;
1598 int prev_family;
1599 int peer_addr_updated = 0;
1600
1601 if (!peer->conf_if)
1602 return;
1603
d12b745c
DS
1604 /*
1605 * Our peer structure is stored in the bgp->peerhash
1606 * release it before we modify anything.
1607 */
1608 hash_release(peer->bgp->peerhash, peer);
1609
d62a17ae 1610 prev_family = peer->su.sa.sa_family;
a36898e7 1611 if ((ifp = if_lookup_by_name(peer->conf_if, peer->bgp->vrf_id))) {
d62a17ae 1612 peer->ifp = ifp;
1613 /* If BGP unnumbered is not "v6only", we first see if we can
1614 * derive the
1615 * peer's IPv4 address.
1616 */
1617 if (!CHECK_FLAG(peer->flags, PEER_FLAG_IFPEER_V6ONLY))
1618 peer_addr_updated =
1619 bgp_peer_conf_if_to_su_update_v4(peer, ifp);
1620
1621 /* If "v6only" or we can't derive peer's IPv4 address, see if
1622 * we've
1623 * learnt the peer's IPv6 link-local address. This is from the
1624 * source
1625 * IPv6 address in router advertisement.
1626 */
1627 if (!peer_addr_updated)
1628 peer_addr_updated =
1629 bgp_peer_conf_if_to_su_update_v6(peer, ifp);
1630 }
1631 /* If we could derive the peer address, we may need to install the
1632 * password
1633 * configured for the peer, if any, on the listen socket. Otherwise,
1634 * mark
1635 * that peer's address is not available and uninstall the password, if
1636 * needed.
1637 */
1638 if (peer_addr_updated) {
a14810f4
PM
1639 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD)
1640 && prev_family == AF_UNSPEC)
d62a17ae 1641 bgp_md5_set(peer);
1642 } else {
a14810f4
PM
1643 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD)
1644 && prev_family != AF_UNSPEC)
d62a17ae 1645 bgp_md5_unset(peer);
1646 peer->su.sa.sa_family = AF_UNSPEC;
1647 memset(&peer->su.sin6.sin6_addr, 0, sizeof(struct in6_addr));
1648 }
1649
d12b745c
DS
1650 /*
1651 * Since our su changed we need to del/add peer to the peerhash
1652 */
8e3aae66 1653 (void)hash_get(peer->bgp->peerhash, peer, hash_alloc_intern);
a80beece
DS
1654}
1655
1479ed2f 1656void bgp_recalculate_afi_safi_bestpaths(struct bgp *bgp, afi_t afi, safi_t safi)
318cac96 1657{
9bcb3eef 1658 struct bgp_dest *dest, *ndest;
67009e22 1659 struct bgp_table *table;
318cac96 1660
9bcb3eef
DS
1661 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
1662 dest = bgp_route_next(dest)) {
1663 table = bgp_dest_get_bgp_table_info(dest);
67009e22 1664 if (table != NULL) {
318cac96
DW
1665 /* Special handling for 2-level routing
1666 * tables. */
a4d82a8a 1667 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
318cac96 1668 || safi == SAFI_EVPN) {
9bcb3eef
DS
1669 for (ndest = bgp_table_top(table); ndest;
1670 ndest = bgp_route_next(ndest))
1671 bgp_process(bgp, ndest, afi, safi);
318cac96 1672 } else
9bcb3eef 1673 bgp_process(bgp, dest, afi, safi);
318cac96
DW
1674 }
1675 }
1676}
1677
06370dac
DW
1678/* Force a bestpath recalculation for all prefixes. This is used
1679 * when 'bgp bestpath' commands are entered.
1680 */
d62a17ae 1681void bgp_recalculate_all_bestpaths(struct bgp *bgp)
1682{
1683 afi_t afi;
1684 safi_t safi;
d62a17ae 1685
05c7a1cc
QY
1686 FOREACH_AFI_SAFI (afi, safi) {
1687 bgp_recalculate_afi_safi_bestpaths(bgp, afi, safi);
d62a17ae 1688 }
06370dac
DW
1689}
1690
19bd3dff
DS
1691/*
1692 * Create new BGP peer.
1693 *
1694 * conf_if and su are mutually exclusive if configuring from the cli.
1695 * If we are handing a doppelganger, then we *must* pass in both
1696 * the original peer's su and conf_if, so that we can appropriately
1697 * track the bgp->peerhash( ie we don't want to remove the current
1698 * one from the config ).
1699 */
d62a17ae 1700struct peer *peer_create(union sockunion *su, const char *conf_if,
1701 struct bgp *bgp, as_t local_as, as_t remote_as,
e84c59af 1702 int as_type, struct peer_group *group)
d62a17ae 1703{
1704 int active;
1705 struct peer *peer;
1706 char buf[SU_ADDRSTRLEN];
e84c59af
DA
1707 afi_t afi;
1708 safi_t safi;
d62a17ae 1709
1710 peer = peer_new(bgp);
1711 if (conf_if) {
1712 peer->conf_if = XSTRDUP(MTYPE_PEER_CONF_IF, conf_if);
19bd3dff
DS
1713 if (su)
1714 peer->su = *su;
1715 else
1716 bgp_peer_conf_if_to_su_update(peer);
0a22ddfb 1717 XFREE(MTYPE_BGP_PEER_HOST, peer->host);
d62a17ae 1718 peer->host = XSTRDUP(MTYPE_BGP_PEER_HOST, conf_if);
1719 } else if (su) {
1720 peer->su = *su;
1721 sockunion2str(su, buf, SU_ADDRSTRLEN);
0a22ddfb 1722 XFREE(MTYPE_BGP_PEER_HOST, peer->host);
d62a17ae 1723 peer->host = XSTRDUP(MTYPE_BGP_PEER_HOST, buf);
1724 }
1725 peer->local_as = local_as;
1726 peer->as = remote_as;
1727 peer->as_type = as_type;
1728 peer->local_id = bgp->router_id;
1729 peer->v_holdtime = bgp->default_holdtime;
1730 peer->v_keepalive = bgp->default_keepalive;
b90a8e13
PM
1731 peer->v_routeadv = (peer_sort(peer) == BGP_PEER_IBGP)
1732 ? BGP_DEFAULT_IBGP_ROUTEADV
1733 : BGP_DEFAULT_EBGP_ROUTEADV;
aa24a36a
DA
1734 if (bgp_config_inprocess())
1735 peer->shut_during_cfg = true;
d62a17ae 1736
1737 peer = peer_lock(peer); /* bgp peer list reference */
1738 peer->group = group;
1739 listnode_add_sort(bgp->peer, peer);
8e3aae66 1740 (void)hash_get(bgp->peerhash, peer, hash_alloc_intern);
d62a17ae 1741
4961a5a2 1742 /* Adjust update-group coalesce timer heuristics for # peers. */
37a333fe 1743 if (bgp->heuristic_coalesce) {
14b8641a
QY
1744 long ct = BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1745 + (bgp->peer->count
1746 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME);
1747 bgp->coalesce_time = MIN(BGP_MAX_SUBGROUP_COALESCE_TIME, ct);
1748 }
4961a5a2 1749
d62a17ae 1750 active = peer_active(peer);
05912a17
DD
1751 if (!active) {
1752 if (peer->su.sa.sa_family == AF_UNSPEC)
1753 peer->last_reset = PEER_DOWN_NBR_ADDR;
1754 else
1755 peer->last_reset = PEER_DOWN_NOAFI_ACTIVATED;
1756 }
d62a17ae 1757
1758 /* Last read and reset time set */
1759 peer->readtime = peer->resettime = bgp_clock();
1760
1761 /* Default TTL set. */
a0903cd2 1762 peer->ttl = (peer->sort == BGP_PEER_IBGP) ? MAXTTL : BGP_DEFAULT_TTL;
d62a17ae 1763
8336c896
DA
1764 /* Default configured keepalives count for shutdown rtt command */
1765 peer->rtt_keepalive_conf = 1;
1766
d62a17ae 1767 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
1768
b16bcbba
TA
1769 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1770 * neighbor for the corresponding address family. IPv4 Unicast is
1771 * the only address family enabled by default without expliict
1772 * configuration.
e84c59af
DA
1773 */
1774 FOREACH_AFI_SAFI (afi, safi) {
38d11af5
TA
1775 if (bgp->default_af[afi][safi]) {
1776 peer->afc[afi][safi] = 1;
1777 peer_af_create(peer, afi, safi);
e84c59af 1778 }
d62a17ae 1779 }
1780
f26845f9
QY
1781 /* auto shutdown if configured */
1782 if (bgp->autoshutdown)
1783 peer_flag_set(peer, PEER_FLAG_SHUTDOWN);
d62a17ae 1784 /* Set up peer's events and timers. */
f26845f9 1785 else if (!active && peer_active(peer))
d62a17ae 1786 bgp_timer_set(peer);
1787
794b37d5 1788 bgp_peer_gr_flags_update(peer);
36235319 1789 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
dc95985f 1790
d62a17ae 1791 return peer;
718e3744 1792}
1793
dd793e4a 1794/* Make accept BGP peer. This function is only called from the test code */
d62a17ae 1795struct peer *peer_create_accept(struct bgp *bgp)
eb821189 1796{
d62a17ae 1797 struct peer *peer;
eb821189 1798
d62a17ae 1799 peer = peer_new(bgp);
1ff9a340 1800
d62a17ae 1801 peer = peer_lock(peer); /* bgp peer list reference */
1802 listnode_add_sort(bgp->peer, peer);
eb821189 1803
d62a17ae 1804 return peer;
eb821189 1805}
1806
318cac96
DW
1807/*
1808 * Return true if we have a peer configured to use this afi/safi
1809 */
1810int bgp_afi_safi_peer_exists(struct bgp *bgp, afi_t afi, safi_t safi)
1811{
1812 struct listnode *node;
1813 struct peer *peer;
1814
1815 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
1816 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
1817 continue;
1818
1819 if (peer->afc[afi][safi])
1820 return 1;
1821 }
1822
1823 return 0;
1824}
1825
718e3744 1826/* Change peer's AS number. */
d62a17ae 1827void peer_as_change(struct peer *peer, as_t as, int as_specified)
1828{
3061acc2 1829 enum bgp_peer_sort origtype, newtype;
d62a17ae 1830
1831 /* Stop peer. */
1832 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
1833 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
1834 peer->last_reset = PEER_DOWN_REMOTE_AS_CHANGE;
1835 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1836 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
1837 } else
1838 bgp_session_reset(peer);
1839 }
469cbb0f 1840 origtype = peer_sort_lookup(peer);
d62a17ae 1841 peer->as = as;
1842 peer->as_type = as_specified;
1843
1844 if (bgp_config_check(peer->bgp, BGP_CONFIG_CONFEDERATION)
1845 && !bgp_confederation_peers_check(peer->bgp, as)
1846 && peer->bgp->as != as)
1847 peer->local_as = peer->bgp->confed_id;
1848 else
1849 peer->local_as = peer->bgp->as;
1850
469cbb0f 1851 newtype = peer_sort(peer);
d62a17ae 1852 /* Advertisement-interval reset */
b90a8e13 1853 if (!CHECK_FLAG(peer->flags, PEER_FLAG_ROUTEADV)) {
469cbb0f 1854 peer->v_routeadv = (newtype == BGP_PEER_IBGP)
b90a8e13
PM
1855 ? BGP_DEFAULT_IBGP_ROUTEADV
1856 : BGP_DEFAULT_EBGP_ROUTEADV;
d62a17ae 1857 }
b90a8e13 1858
d62a17ae 1859 /* TTL reset */
469cbb0f 1860 if (newtype == BGP_PEER_IBGP)
d62a17ae 1861 peer->ttl = MAXTTL;
469cbb0f 1862 else if (origtype == BGP_PEER_IBGP)
a0903cd2 1863 peer->ttl = BGP_DEFAULT_TTL;
d62a17ae 1864
1865 /* reflector-client reset */
469cbb0f 1866 if (newtype != BGP_PEER_IBGP) {
d62a17ae 1867 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_UNICAST],
1868 PEER_FLAG_REFLECTOR_CLIENT);
1869 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_MULTICAST],
1870 PEER_FLAG_REFLECTOR_CLIENT);
1871 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_LABELED_UNICAST],
1872 PEER_FLAG_REFLECTOR_CLIENT);
1873 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_MPLS_VPN],
1874 PEER_FLAG_REFLECTOR_CLIENT);
1875 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_ENCAP],
1876 PEER_FLAG_REFLECTOR_CLIENT);
7c40bf39 1877 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_FLOWSPEC],
1878 PEER_FLAG_REFLECTOR_CLIENT);
d62a17ae 1879 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_UNICAST],
1880 PEER_FLAG_REFLECTOR_CLIENT);
1881 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_MULTICAST],
1882 PEER_FLAG_REFLECTOR_CLIENT);
1883 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_LABELED_UNICAST],
1884 PEER_FLAG_REFLECTOR_CLIENT);
1885 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_MPLS_VPN],
1886 PEER_FLAG_REFLECTOR_CLIENT);
1887 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_ENCAP],
1888 PEER_FLAG_REFLECTOR_CLIENT);
7c40bf39 1889 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_FLOWSPEC],
1890 PEER_FLAG_REFLECTOR_CLIENT);
d62a17ae 1891 UNSET_FLAG(peer->af_flags[AFI_L2VPN][SAFI_EVPN],
1892 PEER_FLAG_REFLECTOR_CLIENT);
1893 }
1894
1895 /* local-as reset */
469cbb0f 1896 if (newtype != BGP_PEER_EBGP) {
d62a17ae 1897 peer->change_local_as = 0;
a14810f4
PM
1898 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS);
1899 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
1900 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
d62a17ae 1901 }
718e3744 1902}
1903
1904/* If peer does not exist, create new one. If peer already exists,
1905 set AS number to the peer. */
d62a17ae 1906int peer_remote_as(struct bgp *bgp, union sockunion *su, const char *conf_if,
e84c59af 1907 as_t *as, int as_type)
d62a17ae 1908{
1909 struct peer *peer;
1910 as_t local_as;
1911
1912 if (conf_if)
1913 peer = peer_lookup_by_conf_if(bgp, conf_if);
1914 else
1915 peer = peer_lookup(bgp, su);
1916
1917 if (peer) {
1918 /* Not allowed for a dynamic peer. */
1919 if (peer_dynamic_neighbor(peer)) {
1920 *as = peer->as;
1921 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER;
1922 }
718e3744 1923
d62a17ae 1924 /* When this peer is a member of peer-group. */
1925 if (peer->group) {
faa16034
DS
1926 /* peer-group already has AS number/internal/external */
1927 if (peer->group->conf->as
1928 || peer->group->conf->as_type) {
d62a17ae 1929 /* Return peer group's AS number. */
1930 *as = peer->group->conf->as;
1931 return BGP_ERR_PEER_GROUP_MEMBER;
1932 }
faa16034 1933
3061acc2
DA
1934 enum bgp_peer_sort peer_sort_type =
1935 peer_sort(peer->group->conf);
faa16034
DS
1936
1937 /* Explicit AS numbers used, compare AS numbers */
1938 if (as_type == AS_SPECIFIED) {
1939 if (((peer_sort_type == BGP_PEER_IBGP)
1940 && (bgp->as != *as))
1941 || ((peer_sort_type == BGP_PEER_EBGP)
1942 && (bgp->as == *as))) {
d62a17ae 1943 *as = peer->as;
1944 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
1945 }
1946 } else {
faa16034
DS
1947 /* internal/external used, compare as-types */
1948 if (((peer_sort_type == BGP_PEER_IBGP)
1949 && (as_type != AS_INTERNAL))
1950 || ((peer_sort_type == BGP_PEER_EBGP)
1951 && (as_type != AS_EXTERNAL))) {
d62a17ae 1952 *as = peer->as;
1953 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
1954 }
1955 }
1956 }
93406d87 1957
d62a17ae 1958 /* Existing peer's AS number change. */
1959 if (((peer->as_type == AS_SPECIFIED) && peer->as != *as)
1960 || (peer->as_type != as_type))
1961 peer_as_change(peer, *as, as_type);
1962 } else {
1963 if (conf_if)
1964 return BGP_ERR_NO_INTERFACE_CONFIG;
1965
1966 /* If the peer is not part of our confederation, and its not an
1967 iBGP peer then spoof the source AS */
1968 if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)
1969 && !bgp_confederation_peers_check(bgp, *as)
1970 && bgp->as != *as)
1971 local_as = bgp->confed_id;
1972 else
1973 local_as = bgp->as;
1974
e84c59af 1975 peer_create(su, conf_if, bgp, local_as, *as, as_type, NULL);
d62a17ae 1976 }
718e3744 1977
d62a17ae 1978 return 0;
1979}
718e3744 1980
9790f44f
DS
1981static void peer_group2peer_config_copy_af(struct peer_group *group,
1982 struct peer *peer, afi_t afi,
1983 safi_t safi)
1984{
1985 int in = FILTER_IN;
1986 int out = FILTER_OUT;
4c391142 1987 uint32_t flags_tmp;
e7103a96
PM
1988 uint32_t pflags_ovrd;
1989 uint8_t *pfilter_ovrd;
9790f44f 1990 struct peer *conf;
9790f44f
DS
1991
1992 conf = group->conf;
e7103a96
PM
1993 pflags_ovrd = peer->af_flags_override[afi][safi];
1994 pfilter_ovrd = &peer->filter_override[afi][safi][in];
9790f44f
DS
1995
1996 /* peer af_flags apply */
4c391142
PM
1997 flags_tmp = conf->af_flags[afi][safi] & ~pflags_ovrd;
1998 flags_tmp ^= conf->af_flags_invert[afi][safi]
1999 ^ peer->af_flags_invert[afi][safi];
2000 flags_tmp &= ~pflags_ovrd;
2001
2002 UNSET_FLAG(peer->af_flags[afi][safi], ~pflags_ovrd);
2003 SET_FLAG(peer->af_flags[afi][safi], flags_tmp);
2004 SET_FLAG(peer->af_flags_invert[afi][safi],
2005 conf->af_flags_invert[afi][safi]);
9790f44f
DS
2006
2007 /* maximum-prefix */
e7103a96
PM
2008 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_MAX_PREFIX)) {
2009 PEER_ATTR_INHERIT(peer, group, pmax[afi][safi]);
2010 PEER_ATTR_INHERIT(peer, group, pmax_threshold[afi][safi]);
2011 PEER_ATTR_INHERIT(peer, group, pmax_restart[afi][safi]);
2012 }
9790f44f 2013
80444d30
LS
2014 /* maximum-prefix-out */
2015 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_MAX_PREFIX_OUT))
2016 PEER_ATTR_INHERIT(peer, group, pmax_out[afi][safi]);
2017
9790f44f 2018 /* allowas-in */
e7103a96
PM
2019 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_ALLOWAS_IN))
2020 PEER_ATTR_INHERIT(peer, group, allowas_in[afi][safi]);
9790f44f
DS
2021
2022 /* weight */
e7103a96
PM
2023 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_WEIGHT))
2024 PEER_ATTR_INHERIT(peer, group, weight[afi][safi]);
9790f44f
DS
2025
2026 /* default-originate route-map */
e7103a96
PM
2027 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_DEFAULT_ORIGINATE)) {
2028 PEER_STR_ATTR_INHERIT(peer, group, default_rmap[afi][safi].name,
2029 MTYPE_ROUTE_MAP_NAME);
2030 PEER_ATTR_INHERIT(peer, group, default_rmap[afi][safi].map);
9790f44f
DS
2031 }
2032
2033 /* inbound filter apply */
e7103a96
PM
2034 if (!CHECK_FLAG(pfilter_ovrd[in], PEER_FT_DISTRIBUTE_LIST)) {
2035 PEER_STR_ATTR_INHERIT(peer, group,
2036 filter[afi][safi].dlist[in].name,
2037 MTYPE_BGP_FILTER_NAME);
2038 PEER_ATTR_INHERIT(peer, group,
2039 filter[afi][safi].dlist[in].alist);
9790f44f
DS
2040 }
2041
e7103a96
PM
2042 if (!CHECK_FLAG(pfilter_ovrd[in], PEER_FT_PREFIX_LIST)) {
2043 PEER_STR_ATTR_INHERIT(peer, group,
2044 filter[afi][safi].plist[in].name,
2045 MTYPE_BGP_FILTER_NAME);
2046 PEER_ATTR_INHERIT(peer, group,
2047 filter[afi][safi].plist[in].plist);
9790f44f
DS
2048 }
2049
e7103a96
PM
2050 if (!CHECK_FLAG(pfilter_ovrd[in], PEER_FT_FILTER_LIST)) {
2051 PEER_STR_ATTR_INHERIT(peer, group,
2052 filter[afi][safi].aslist[in].name,
2053 MTYPE_BGP_FILTER_NAME);
2054 PEER_ATTR_INHERIT(peer, group,
2055 filter[afi][safi].aslist[in].aslist);
9790f44f
DS
2056 }
2057
e7103a96
PM
2058 if (!CHECK_FLAG(pfilter_ovrd[RMAP_IN], PEER_FT_ROUTE_MAP)) {
2059 PEER_STR_ATTR_INHERIT(peer, group,
2060 filter[afi][safi].map[in].name,
2061 MTYPE_BGP_FILTER_NAME);
2062 PEER_ATTR_INHERIT(peer, group,
2063 filter[afi][safi].map[RMAP_IN].map);
9790f44f
DS
2064 }
2065
2066 /* outbound filter apply */
e7103a96
PM
2067 if (!CHECK_FLAG(pfilter_ovrd[out], PEER_FT_DISTRIBUTE_LIST)) {
2068 PEER_STR_ATTR_INHERIT(peer, group,
2069 filter[afi][safi].dlist[out].name,
2070 MTYPE_BGP_FILTER_NAME);
2071 PEER_ATTR_INHERIT(peer, group,
2072 filter[afi][safi].dlist[out].alist);
9790f44f
DS
2073 }
2074
e7103a96
PM
2075 if (!CHECK_FLAG(pfilter_ovrd[out], PEER_FT_PREFIX_LIST)) {
2076 PEER_STR_ATTR_INHERIT(peer, group,
2077 filter[afi][safi].plist[out].name,
2078 MTYPE_BGP_FILTER_NAME);
2079 PEER_ATTR_INHERIT(peer, group,
2080 filter[afi][safi].plist[out].plist);
9790f44f
DS
2081 }
2082
e7103a96
PM
2083 if (!CHECK_FLAG(pfilter_ovrd[out], PEER_FT_FILTER_LIST)) {
2084 PEER_STR_ATTR_INHERIT(peer, group,
2085 filter[afi][safi].aslist[out].name,
2086 MTYPE_BGP_FILTER_NAME);
2087 PEER_ATTR_INHERIT(peer, group,
2088 filter[afi][safi].aslist[out].aslist);
2089 }
2090
2091 if (!CHECK_FLAG(pfilter_ovrd[RMAP_OUT], PEER_FT_ROUTE_MAP)) {
2092 PEER_STR_ATTR_INHERIT(peer, group,
2093 filter[afi][safi].map[RMAP_OUT].name,
2094 MTYPE_BGP_FILTER_NAME);
2095 PEER_ATTR_INHERIT(peer, group,
2096 filter[afi][safi].map[RMAP_OUT].map);
2097 }
2098
2099 /* nondirectional filter apply */
2100 if (!CHECK_FLAG(pfilter_ovrd[0], PEER_FT_UNSUPPRESS_MAP)) {
2101 PEER_STR_ATTR_INHERIT(peer, group, filter[afi][safi].usmap.name,
2102 MTYPE_BGP_FILTER_NAME);
2103 PEER_ATTR_INHERIT(peer, group, filter[afi][safi].usmap.map);
9790f44f 2104 }
dcc68b5e 2105
9118301e
DA
2106 /* Conditional Advertisements */
2107 if (!CHECK_FLAG(pfilter_ovrd[RMAP_OUT], PEER_FT_ADVERTISE_MAP)) {
2108 PEER_STR_ATTR_INHERIT(peer, group,
2109 filter[afi][safi].advmap.aname,
2110 MTYPE_BGP_FILTER_NAME);
2111 PEER_ATTR_INHERIT(peer, group, filter[afi][safi].advmap.amap);
2112 PEER_STR_ATTR_INHERIT(peer, group,
2113 filter[afi][safi].advmap.cname,
2114 MTYPE_BGP_FILTER_NAME);
2115 PEER_ATTR_INHERIT(peer, group, filter[afi][safi].advmap.cmap);
2116 PEER_ATTR_INHERIT(peer, group,
2117 filter[afi][safi].advmap.condition);
2118 }
2119
dcc68b5e
MS
2120 if (peer->addpath_type[afi][safi] == BGP_ADDPATH_NONE) {
2121 peer->addpath_type[afi][safi] = conf->addpath_type[afi][safi];
2122 bgp_addpath_type_changed(conf->bgp);
2123 }
9790f44f
DS
2124}
2125
2126static int peer_activate_af(struct peer *peer, afi_t afi, safi_t safi)
d62a17ae 2127{
2128 int active;
9bf904cc 2129 struct peer *other;
718e3744 2130
d62a17ae 2131 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
e50f7cfd 2132 flog_err(EC_BGP_PEER_GROUP, "%s was called for peer-group %s",
1c50c1c0 2133 __func__, peer->host);
d62a17ae 2134 return 1;
2135 }
3f9c7369 2136
d62a17ae 2137 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2138 */
2139 if ((safi == SAFI_UNICAST && peer->afc[afi][SAFI_LABELED_UNICAST])
2140 || (safi == SAFI_LABELED_UNICAST && peer->afc[afi][SAFI_UNICAST]))
2141 return BGP_ERR_PEER_SAFI_CONFLICT;
2142
2143 /* Nothing to do if we've already activated this peer */
2144 if (peer->afc[afi][safi])
2145 return 0;
2146
2147 if (peer_af_create(peer, afi, safi) == NULL)
2148 return 1;
2149
2150 active = peer_active(peer);
2151 peer->afc[afi][safi] = 1;
2152
9790f44f 2153 if (peer->group)
a4d82a8a 2154 peer_group2peer_config_copy_af(peer->group, peer, afi, safi);
9790f44f 2155
d62a17ae 2156 if (!active && peer_active(peer)) {
2157 bgp_timer_set(peer);
2158 } else {
feb17238 2159 if (peer_established(peer)) {
d62a17ae 2160 if (CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_RCV)) {
2161 peer->afc_adv[afi][safi] = 1;
2162 bgp_capability_send(peer, afi, safi,
2163 CAPABILITY_CODE_MP,
2164 CAPABILITY_ACTION_SET);
2165 if (peer->afc_recv[afi][safi]) {
2166 peer->afc_nego[afi][safi] = 1;
e1a32ec1
DS
2167 bgp_announce_route(peer, afi, safi,
2168 false);
d62a17ae 2169 }
2170 } else {
2171 peer->last_reset = PEER_DOWN_AF_ACTIVATE;
2172 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2173 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2174 }
2175 }
64c1a9b5
DS
2176 if (peer->status == OpenSent || peer->status == OpenConfirm) {
2177 peer->last_reset = PEER_DOWN_AF_ACTIVATE;
2178 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2179 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2180 }
9bf904cc
DS
2181 /*
2182 * If we are turning on a AFI/SAFI locally and we've
2183 * started bringing a peer up, we need to tell
2184 * the other peer to restart because we might loose
2185 * configuration here because when the doppelganger
2186 * gets to a established state due to how
2187 * we resolve we could just overwrite the afi/safi
2188 * activation.
2189 */
2190 other = peer->doppelganger;
2191 if (other
2192 && (other->status == OpenSent
2193 || other->status == OpenConfirm)) {
2194 other->last_reset = PEER_DOWN_AF_ACTIVATE;
2195 bgp_notify_send(other, BGP_NOTIFY_CEASE,
2196 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2197 }
d62a17ae 2198 }
04b6bdc0 2199
d62a17ae 2200 return 0;
2201}
9e08021c 2202
d62a17ae 2203/* Activate the peer or peer group for specified AFI and SAFI. */
2204int peer_activate(struct peer *peer, afi_t afi, safi_t safi)
2205{
2206 int ret = 0;
2207 struct peer_group *group;
2208 struct listnode *node, *nnode;
2209 struct peer *tmp_peer;
318cac96 2210 struct bgp *bgp;
d62a17ae 2211
2212 /* Nothing to do if we've already activated this peer */
2213 if (peer->afc[afi][safi])
2214 return ret;
2215
318cac96
DW
2216 bgp = peer->bgp;
2217
d62a17ae 2218 /* This is a peer-group so activate all of the members of the
2219 * peer-group as well */
2220 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2221
2222 /* Do not activate a peer for both SAFI_UNICAST and
2223 * SAFI_LABELED_UNICAST */
2224 if ((safi == SAFI_UNICAST
2225 && peer->afc[afi][SAFI_LABELED_UNICAST])
2226 || (safi == SAFI_LABELED_UNICAST
2227 && peer->afc[afi][SAFI_UNICAST]))
2228 return BGP_ERR_PEER_SAFI_CONFLICT;
2229
2230 peer->afc[afi][safi] = 1;
2231 group = peer->group;
2232
2233 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, tmp_peer)) {
9790f44f 2234 ret |= peer_activate_af(tmp_peer, afi, safi);
d62a17ae 2235 }
2236 } else {
9790f44f 2237 ret |= peer_activate_af(peer, afi, safi);
d62a17ae 2238 }
718e3744 2239
a4d82a8a
PZ
2240 /* If this is the first peer to be activated for this
2241 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
0f3ac819 2242 if (ret != BGP_ERR_PEER_SAFI_CONFLICT && safi == SAFI_LABELED_UNICAST
a4d82a8a 2243 && !bgp->allocate_mpls_labels[afi][SAFI_UNICAST]) {
318cac96
DW
2244
2245 if (BGP_DEBUG(zebra, ZEBRA))
4cb5e18b 2246 zlog_debug(
a4d82a8a 2247 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
318cac96
DW
2248
2249 bgp->allocate_mpls_labels[afi][SAFI_UNICAST] = 1;
2250 bgp_recalculate_afi_safi_bestpaths(bgp, afi, SAFI_UNICAST);
2251 }
2252
b98f7728
PG
2253 if (safi == SAFI_FLOWSPEC) {
2254 /* connect to table manager */
6818e7e5 2255 bgp_zebra_init_tm_connect(bgp);
b98f7728 2256 }
d62a17ae 2257 return ret;
718e3744 2258}
6b0655a2 2259
3dc339cd
DA
2260static bool non_peergroup_deactivate_af(struct peer *peer, afi_t afi,
2261 safi_t safi)
718e3744 2262{
d62a17ae 2263 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
e50f7cfd 2264 flog_err(EC_BGP_PEER_GROUP, "%s was called for peer-group %s",
1c50c1c0 2265 __func__, peer->host);
3dc339cd 2266 return true;
d62a17ae 2267 }
718e3744 2268
d62a17ae 2269 /* Nothing to do if we've already deactivated this peer */
2270 if (!peer->afc[afi][safi])
3dc339cd 2271 return false;
718e3744 2272
d62a17ae 2273 /* De-activate the address family configuration. */
2274 peer->afc[afi][safi] = 0;
718e3744 2275
d62a17ae 2276 if (peer_af_delete(peer, afi, safi) != 0) {
e50f7cfd 2277 flog_err(EC_BGP_PEER_DELETE,
601837df
DS
2278 "couldn't delete af structure for peer %s(%s, %s)",
2279 peer->host, afi2str(afi), safi2str(safi));
3dc339cd 2280 return true;
d62a17ae 2281 }
718e3744 2282
feb17238 2283 if (peer_established(peer)) {
d62a17ae 2284 if (CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_RCV)) {
2285 peer->afc_adv[afi][safi] = 0;
2286 peer->afc_nego[afi][safi] = 0;
2287
2288 if (peer_active_nego(peer)) {
2289 bgp_capability_send(peer, afi, safi,
2290 CAPABILITY_CODE_MP,
2291 CAPABILITY_ACTION_UNSET);
2292 bgp_clear_route(peer, afi, safi);
2293 peer->pcount[afi][safi] = 0;
2294 } else {
2295 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
2296 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2297 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2298 }
2299 } else {
2300 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
2301 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2302 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2303 }
2304 }
718e3744 2305
3dc339cd 2306 return false;
d62a17ae 2307}
718e3744 2308
d62a17ae 2309int peer_deactivate(struct peer *peer, afi_t afi, safi_t safi)
718e3744 2310{
d62a17ae 2311 int ret = 0;
2312 struct peer_group *group;
2313 struct peer *tmp_peer;
2314 struct listnode *node, *nnode;
318cac96 2315 struct bgp *bgp;
718e3744 2316
d62a17ae 2317 /* Nothing to do if we've already de-activated this peer */
2318 if (!peer->afc[afi][safi])
2319 return ret;
718e3744 2320
d62a17ae 2321 /* This is a peer-group so de-activate all of the members of the
2322 * peer-group as well */
2323 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2324 peer->afc[afi][safi] = 0;
2325 group = peer->group;
718e3744 2326
d62a17ae 2327 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, tmp_peer)) {
2328 ret |= non_peergroup_deactivate_af(tmp_peer, afi, safi);
2329 }
2330 } else {
2331 ret |= non_peergroup_deactivate_af(peer, afi, safi);
2332 }
718e3744 2333
318cac96
DW
2334 bgp = peer->bgp;
2335
a4d82a8a
PZ
2336 /* If this is the last peer to be deactivated for this
2337 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2338 if (safi == SAFI_LABELED_UNICAST
2339 && bgp->allocate_mpls_labels[afi][SAFI_UNICAST]
2340 && !bgp_afi_safi_peer_exists(bgp, afi, safi)) {
318cac96
DW
2341
2342 if (BGP_DEBUG(zebra, ZEBRA))
4cb5e18b 2343 zlog_debug(
a4d82a8a 2344 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
318cac96
DW
2345
2346 bgp->allocate_mpls_labels[afi][SAFI_UNICAST] = 0;
2347 bgp_recalculate_afi_safi_bestpaths(bgp, afi, SAFI_UNICAST);
2348 }
d62a17ae 2349 return ret;
2350}
718e3744 2351
d7b3cda6 2352void peer_nsf_stop(struct peer *peer)
718e3744 2353{
d62a17ae 2354 afi_t afi;
2355 safi_t safi;
718e3744 2356
d62a17ae 2357 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2358 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
718e3744 2359
df8d723c
DA
2360 FOREACH_AFI_SAFI_NSF (afi, safi)
2361 peer->nsf[afi][safi] = 0;
d62a17ae 2362
2363 if (peer->t_gr_restart) {
2364 BGP_TIMER_OFF(peer->t_gr_restart);
2365 if (bgp_debug_neighbor_events(peer))
f70c91dc 2366 zlog_debug("%pBP graceful restart timer stopped", peer);
1ff9a340 2367 }
d62a17ae 2368 if (peer->t_gr_stale) {
2369 BGP_TIMER_OFF(peer->t_gr_stale);
2370 if (bgp_debug_neighbor_events(peer))
2371 zlog_debug(
f70c91dc
DA
2372 "%pBP graceful restart stalepath timer stopped",
2373 peer);
d62a17ae 2374 }
2375 bgp_clear_route_all(peer);
2376}
718e3744 2377
d62a17ae 2378/* Delete peer from confguration.
2379 *
2380 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2381 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2382 *
2383 * This function /should/ take care to be idempotent, to guard against
2384 * it being called multiple times through stray events that come in
2385 * that happen to result in this function being called again. That
2386 * said, getting here for a "Deleted" peer is a bug in the neighbour
2387 * FSM.
2388 */
2389int peer_delete(struct peer *peer)
2390{
2391 int i;
2392 afi_t afi;
2393 safi_t safi;
2394 struct bgp *bgp;
2395 struct bgp_filter *filter;
2396 struct listnode *pn;
2397 int accept_peer;
f14e6fdb 2398
d62a17ae 2399 assert(peer->status != Deleted);
718e3744 2400
d62a17ae 2401 bgp = peer->bgp;
2402 accept_peer = CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
080b97f2 2403
46aeabed
LS
2404 bgp_soft_reconfig_table_task_cancel(bgp, NULL, peer);
2405
0a6469e5 2406 bgp_keepalives_off(peer);
424ab01d
QY
2407 bgp_reads_off(peer);
2408 bgp_writes_off(peer);
2409 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
2410 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
0a6469e5 2411 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON));
424ab01d 2412
d62a17ae 2413 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT))
2414 peer_nsf_stop(peer);
718e3744 2415
d62a17ae 2416 SET_FLAG(peer->flags, PEER_FLAG_DELETE);
718e3744 2417
21bfce98
RZ
2418 /* Remove BFD settings. */
2419 if (peer->bfd_config)
2420 bgp_peer_remove_bfd_config(peer);
e8ba5293 2421
d62a17ae 2422 /* If this peer belongs to peer group, clear up the
2423 relationship. */
2424 if (peer->group) {
2425 if (peer_dynamic_neighbor(peer))
2426 peer_drop_dynamic_neighbor(peer);
718e3744 2427
d62a17ae 2428 if ((pn = listnode_lookup(peer->group->peer, peer))) {
2429 peer = peer_unlock(
2430 peer); /* group->peer list reference */
2431 list_delete_node(peer->group->peer, pn);
2432 }
2433 peer->group = NULL;
2434 }
718e3744 2435
d62a17ae 2436 /* Withdraw all information from routing table. We can not use
2437 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2438 * executed after peer structure is deleted.
2439 */
2440 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
2441 bgp_stop(peer);
2442 UNSET_FLAG(peer->flags, PEER_FLAG_DELETE);
2443
2444 if (peer->doppelganger) {
2445 peer->doppelganger->doppelganger = NULL;
2446 peer->doppelganger = NULL;
2447 }
718e3744 2448
d62a17ae 2449 UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
2450 bgp_fsm_change_status(peer, Deleted);
718e3744 2451
d62a17ae 2452 /* Remove from NHT */
2453 if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
2454 bgp_unlink_nexthop_by_peer(peer);
1ff9a340 2455
d62a17ae 2456 /* Password configuration */
a14810f4 2457 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD)) {
d62a17ae 2458 XFREE(MTYPE_PEER_PASSWORD, peer->password);
d62a17ae 2459 if (!accept_peer && !BGP_PEER_SU_UNSPEC(peer)
2734ff6b
PR
2460 && !CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
2461 && !CHECK_FLAG(peer->flags, PEER_FLAG_DYNAMIC_NEIGHBOR))
d62a17ae 2462 bgp_md5_unset(peer);
1ff9a340 2463 }
718e3744 2464
d62a17ae 2465 bgp_timer_set(peer); /* stops all timers for Deleted */
718e3744 2466
d62a17ae 2467 /* Delete from all peer list. */
2468 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
2469 && (pn = listnode_lookup(bgp->peer, peer))) {
2470 peer_unlock(peer); /* bgp peer list reference */
2471 list_delete_node(bgp->peer, pn);
2472 hash_release(bgp->peerhash, peer);
2473 }
718e3744 2474
d62a17ae 2475 /* Buffers. */
2476 if (peer->ibuf) {
424ab01d 2477 stream_fifo_free(peer->ibuf);
d62a17ae 2478 peer->ibuf = NULL;
2479 }
f14e6fdb 2480
d62a17ae 2481 if (peer->obuf) {
2482 stream_fifo_free(peer->obuf);
2483 peer->obuf = NULL;
2484 }
f14e6fdb 2485
424ab01d 2486 if (peer->ibuf_work) {
74ffbfe6 2487 ringbuf_del(peer->ibuf_work);
424ab01d
QY
2488 peer->ibuf_work = NULL;
2489 }
2490
2491 if (peer->obuf_work) {
2492 stream_free(peer->obuf_work);
2493 peer->obuf_work = NULL;
d62a17ae 2494 }
f14e6fdb 2495
d62a17ae 2496 if (peer->scratch) {
2497 stream_free(peer->scratch);
2498 peer->scratch = NULL;
2499 }
f14e6fdb 2500
d62a17ae 2501 /* Local and remote addresses. */
2502 if (peer->su_local) {
2503 sockunion_free(peer->su_local);
2504 peer->su_local = NULL;
2505 }
f14e6fdb 2506
d62a17ae 2507 if (peer->su_remote) {
2508 sockunion_free(peer->su_remote);
2509 peer->su_remote = NULL;
2510 }
f14e6fdb 2511
d62a17ae 2512 /* Free filter related memory. */
05c7a1cc
QY
2513 FOREACH_AFI_SAFI (afi, safi) {
2514 filter = &peer->filter[afi][safi];
d62a17ae 2515
05c7a1cc 2516 for (i = FILTER_IN; i < FILTER_MAX; i++) {
e1b36e13
QY
2517 XFREE(MTYPE_BGP_FILTER_NAME, filter->dlist[i].name);
2518 XFREE(MTYPE_BGP_FILTER_NAME, filter->plist[i].name);
2519 XFREE(MTYPE_BGP_FILTER_NAME, filter->aslist[i].name);
05c7a1cc 2520 }
d62a17ae 2521
05c7a1cc 2522 for (i = RMAP_IN; i < RMAP_MAX; i++) {
e1b36e13 2523 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[i].name);
05c7a1cc
QY
2524 }
2525
e1b36e13
QY
2526 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
2527 XFREE(MTYPE_ROUTE_MAP_NAME, peer->default_rmap[afi][safi].name);
05c7a1cc
QY
2528 }
2529
a2addae8
RW
2530 FOREACH_AFI_SAFI (afi, safi)
2531 peer_af_delete(peer, afi, safi);
f14e6fdb 2532
e1b36e13
QY
2533 XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
2534 XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
f14e6fdb 2535
d62a17ae 2536 peer_unlock(peer); /* initial reference */
f14e6fdb 2537
d62a17ae 2538 return 0;
fe7d2a48
DS
2539}
2540
d62a17ae 2541static int peer_group_cmp(struct peer_group *g1, struct peer_group *g2)
2542{
2543 return strcmp(g1->name, g2->name);
6aeb9e78
DS
2544}
2545
d62a17ae 2546/* Peer group cofiguration. */
2547static struct peer_group *peer_group_new(void)
718e3744 2548{
9f5dc319 2549 return XCALLOC(MTYPE_PEER_GROUP, sizeof(struct peer_group));
718e3744 2550}
2551
d62a17ae 2552static void peer_group_free(struct peer_group *group)
718e3744 2553{
d62a17ae 2554 XFREE(MTYPE_PEER_GROUP, group);
718e3744 2555}
2556
d62a17ae 2557struct peer_group *peer_group_lookup(struct bgp *bgp, const char *name)
718e3744 2558{
d62a17ae 2559 struct peer_group *group;
2560 struct listnode *node, *nnode;
2561
2562 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
2563 if (strcmp(group->name, name) == 0)
2564 return group;
2565 }
2566 return NULL;
2567}
2568
2569struct peer_group *peer_group_get(struct bgp *bgp, const char *name)
2570{
2571 struct peer_group *group;
2572 afi_t afi;
38d11af5 2573 safi_t safi;
d62a17ae 2574
2575 group = peer_group_lookup(bgp, name);
2576 if (group)
2577 return group;
2578
2579 group = peer_group_new();
2580 group->bgp = bgp;
0a22ddfb 2581 XFREE(MTYPE_PEER_GROUP_HOST, group->name);
d62a17ae 2582 group->name = XSTRDUP(MTYPE_PEER_GROUP_HOST, name);
2583 group->peer = list_new();
2584 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2585 group->listen_range[afi] = list_new();
2586 group->conf = peer_new(bgp);
38d11af5
TA
2587 FOREACH_AFI_SAFI (afi, safi) {
2588 if (bgp->default_af[afi][safi])
2589 group->conf->afc[afi][safi] = 1;
2590 }
0a22ddfb 2591 XFREE(MTYPE_BGP_PEER_HOST, group->conf->host);
d62a17ae 2592 group->conf->host = XSTRDUP(MTYPE_BGP_PEER_HOST, name);
2593 group->conf->group = group;
2594 group->conf->as = 0;
a0903cd2 2595 group->conf->ttl = BGP_DEFAULT_TTL;
e2521429 2596 group->conf->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
d62a17ae 2597 group->conf->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
d62a17ae 2598 SET_FLAG(group->conf->sflags, PEER_STATUS_GROUP);
2599 listnode_add_sort(bgp->group, group);
718e3744 2600
d62a17ae 2601 return group;
718e3744 2602}
2603
d62a17ae 2604static void peer_group2peer_config_copy(struct peer_group *group,
2605 struct peer *peer)
ad4cbda1 2606{
4c391142 2607 uint32_t flags_tmp;
d62a17ae 2608 struct peer *conf;
ad4cbda1 2609
d62a17ae 2610 conf = group->conf;
ad4cbda1 2611
d62a17ae 2612 /* remote-as */
2613 if (conf->as)
2614 peer->as = conf->as;
718e3744 2615
a14810f4
PM
2616 /* local-as */
2617 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_LOCAL_AS))
d62a17ae 2618 peer->change_local_as = conf->change_local_as;
1758dbaa 2619
a0903cd2
DA
2620 /* If peer-group has configured TTL then override it */
2621 if (conf->ttl != BGP_DEFAULT_TTL)
2622 peer->ttl = conf->ttl;
518f0eb1 2623
d62a17ae 2624 /* GTSM hops */
2625 peer->gtsm_hops = conf->gtsm_hops;
ad12dde6 2626
4c391142
PM
2627 /* peer flags apply */
2628 flags_tmp = conf->flags & ~peer->flags_override;
2629 flags_tmp ^= conf->flags_invert ^ peer->flags_invert;
2630 flags_tmp &= ~peer->flags_override;
2631
2632 UNSET_FLAG(peer->flags, ~peer->flags_override);
2633 SET_FLAG(peer->flags, flags_tmp);
2634 SET_FLAG(peer->flags_invert, conf->flags_invert);
ad4cbda1 2635
d62a17ae 2636 /* peer timers apply */
b90a8e13
PM
2637 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_TIMER)) {
2638 PEER_ATTR_INHERIT(peer, group, holdtime);
2639 PEER_ATTR_INHERIT(peer, group, keepalive);
2640 }
6aeb9e78 2641
b90a8e13
PM
2642 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_TIMER_CONNECT)) {
2643 PEER_ATTR_INHERIT(peer, group, connect);
2644 if (CHECK_FLAG(conf->flags, PEER_FLAG_TIMER_CONNECT))
2645 peer->v_connect = conf->connect;
2646 else
5d5393b9 2647 peer->v_connect = peer->bgp->default_connect_retry;
b90a8e13
PM
2648 }
2649
6c537a18
DS
2650 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_TIMER_DELAYOPEN)) {
2651 PEER_ATTR_INHERIT(peer, group, delayopen);
2652 if (CHECK_FLAG(conf->flags, PEER_FLAG_TIMER_DELAYOPEN))
2653 peer->v_delayopen = conf->delayopen;
2654 else
2655 peer->v_delayopen = peer->bgp->default_delayopen;
2656 }
2657
b90a8e13
PM
2658 /* advertisement-interval apply */
2659 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_ROUTEADV)) {
2660 PEER_ATTR_INHERIT(peer, group, routeadv);
2661 if (CHECK_FLAG(conf->flags, PEER_FLAG_ROUTEADV))
2662 peer->v_routeadv = conf->routeadv;
2663 else
2664 peer->v_routeadv = (peer_sort(peer) == BGP_PEER_IBGP)
2665 ? BGP_DEFAULT_IBGP_ROUTEADV
2666 : BGP_DEFAULT_EBGP_ROUTEADV;
2667 }
718e3744 2668
b3a3290e
DS
2669 /* capability extended-nexthop apply */
2670 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_CAPABILITY_ENHE))
2671 if (CHECK_FLAG(conf->flags, PEER_FLAG_CAPABILITY_ENHE))
2672 SET_FLAG(peer->flags, PEER_FLAG_CAPABILITY_ENHE);
2673
d62a17ae 2674 /* password apply */
a14810f4
PM
2675 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_PASSWORD))
2676 PEER_STR_ATTR_INHERIT(peer, group, password,
2677 MTYPE_PEER_PASSWORD);
718e3744 2678
d62a17ae 2679 if (!BGP_PEER_SU_UNSPEC(peer))
2680 bgp_md5_set(peer);
d4a7a753 2681
d62a17ae 2682 /* update-source apply */
a14810f4
PM
2683 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_UPDATE_SOURCE)) {
2684 if (conf->update_source) {
d62a17ae 2685 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
a14810f4
PM
2686 PEER_SU_ATTR_INHERIT(peer, group, update_source);
2687 } else if (conf->update_if) {
d62a17ae 2688 sockunion_free(peer->update_source);
a14810f4
PM
2689 PEER_STR_ATTR_INHERIT(peer, group, update_if,
2690 MTYPE_PEER_UPDATE_SOURCE);
d62a17ae 2691 }
d62a17ae 2692 }
cd92e548 2693
0c0b85b5
NT
2694 /* Update GR flags for the peer. */
2695 bgp_peer_gr_flags_update(peer);
2696
21bfce98
RZ
2697 /* Apply BFD settings from group to peer if it exists. */
2698 if (conf->bfd_config) {
2699 bgp_peer_configure_bfd(peer, false);
2700 bgp_peer_config_apply(peer, group);
2701 }
ad4cbda1 2702}
718e3744 2703
d62a17ae 2704/* Peer group's remote AS configuration. */
2705int peer_group_remote_as(struct bgp *bgp, const char *group_name, as_t *as,
2706 int as_type)
f14e6fdb 2707{
d62a17ae 2708 struct peer_group *group;
2709 struct peer *peer;
2710 struct listnode *node, *nnode;
f14e6fdb 2711
d62a17ae 2712 group = peer_group_lookup(bgp, group_name);
2713 if (!group)
2714 return -1;
f14e6fdb 2715
d62a17ae 2716 if ((as_type == group->conf->as_type) && (group->conf->as == *as))
2717 return 0;
f14e6fdb 2718
f14e6fdb 2719
d62a17ae 2720 /* When we setup peer-group AS number all peer group member's AS
2721 number must be updated to same number. */
2722 peer_as_change(group->conf, *as, as_type);
f14e6fdb 2723
d62a17ae 2724 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2725 if (((peer->as_type == AS_SPECIFIED) && peer->as != *as)
2726 || (peer->as_type != as_type))
2727 peer_as_change(peer, *as, as_type);
2728 }
f14e6fdb 2729
d62a17ae 2730 return 0;
f14e6fdb
DS
2731}
2732
3dc339cd 2733void peer_notify_unconfig(struct peer *peer)
4e2786df
DA
2734{
2735 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
2736 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2737 BGP_NOTIFY_CEASE_PEER_UNCONFIG);
4e2786df
DA
2738}
2739
3dc339cd 2740void peer_group_notify_unconfig(struct peer_group *group)
4e2786df
DA
2741{
2742 struct peer *peer, *other;
2743 struct listnode *node, *nnode;
2744
2745 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2746 other = peer->doppelganger;
2747 if (other && other->status != Deleted) {
2748 other->group = NULL;
2749 peer_notify_unconfig(other);
2750 } else
2751 peer_notify_unconfig(peer);
2752 }
4e2786df
DA
2753}
2754
d62a17ae 2755int peer_group_delete(struct peer_group *group)
f14e6fdb 2756{
d62a17ae 2757 struct bgp *bgp;
2758 struct peer *peer;
2759 struct prefix *prefix;
2760 struct peer *other;
2761 struct listnode *node, *nnode;
2762 afi_t afi;
f14e6fdb 2763
d62a17ae 2764 bgp = group->bgp;
f14e6fdb 2765
d62a17ae 2766 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2767 other = peer->doppelganger;
b3a3290e
DS
2768
2769 if (CHECK_FLAG(peer->flags, PEER_FLAG_CAPABILITY_ENHE))
2770 bgp_zebra_terminate_radv(bgp, peer);
2771
d62a17ae 2772 peer_delete(peer);
2773 if (other && other->status != Deleted) {
2774 other->group = NULL;
2775 peer_delete(other);
2776 }
2777 }
6a154c88 2778 list_delete(&group->peer);
f14e6fdb 2779
d62a17ae 2780 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2781 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode,
2782 prefix)) {
63265b5c 2783 prefix_free(&prefix);
d62a17ae 2784 }
6a154c88 2785 list_delete(&group->listen_range[afi]);
d62a17ae 2786 }
f14e6fdb 2787
d62a17ae 2788 XFREE(MTYPE_PEER_GROUP_HOST, group->name);
2789 group->name = NULL;
0299c004 2790
21bfce98
RZ
2791 if (group->conf->bfd_config)
2792 bgp_peer_remove_bfd_config(group->conf);
f14e6fdb 2793
d62a17ae 2794 group->conf->group = NULL;
2795 peer_delete(group->conf);
f14e6fdb 2796
d62a17ae 2797 /* Delete from all peer_group list. */
2798 listnode_delete(bgp->group, group);
f14e6fdb 2799
d62a17ae 2800 peer_group_free(group);
f14e6fdb 2801
d62a17ae 2802 return 0;
2803}
f14e6fdb 2804
d62a17ae 2805int peer_group_remote_as_delete(struct peer_group *group)
2806{
2807 struct peer *peer, *other;
2808 struct listnode *node, *nnode;
f14e6fdb 2809
d62a17ae 2810 if ((group->conf->as_type == AS_UNSPECIFIED)
2811 || ((!group->conf->as) && (group->conf->as_type == AS_SPECIFIED)))
2812 return 0;
f14e6fdb 2813
d62a17ae 2814 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2815 other = peer->doppelganger;
f14e6fdb 2816
b3a3290e
DS
2817 if (CHECK_FLAG(peer->flags, PEER_FLAG_CAPABILITY_ENHE))
2818 bgp_zebra_terminate_radv(peer->bgp, peer);
2819
d62a17ae 2820 peer_delete(peer);
f14e6fdb 2821
d62a17ae 2822 if (other && other->status != Deleted) {
2823 other->group = NULL;
2824 peer_delete(other);
2825 }
2826 }
2827 list_delete_all_node(group->peer);
f14e6fdb 2828
d62a17ae 2829 group->conf->as = 0;
2830 group->conf->as_type = AS_UNSPECIFIED;
f14e6fdb 2831
d62a17ae 2832 return 0;
f14e6fdb 2833}
d62a17ae 2834
2835int peer_group_listen_range_add(struct peer_group *group, struct prefix *range)
f14e6fdb 2836{
d62a17ae 2837 struct prefix *prefix;
2838 struct listnode *node, *nnode;
2839 afi_t afi;
f14e6fdb 2840
d62a17ae 2841 afi = family2afi(range->family);
f14e6fdb 2842
d62a17ae 2843 /* Group needs remote AS configured. */
2844 if (group->conf->as_type == AS_UNSPECIFIED)
2845 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS;
718e3744 2846
d62a17ae 2847 /* Ensure no duplicates. Currently we don't care about overlaps. */
2848 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode, prefix)) {
2849 if (prefix_same(range, prefix))
2850 return 0;
2851 }
2852
2853 prefix = prefix_new();
2854 prefix_copy(prefix, range);
2855 listnode_add(group->listen_range[afi], prefix);
9e7d9a61
QY
2856
2857 /* Update passwords for new ranges */
2858 if (group->conf->password)
a4faae3a 2859 bgp_md5_set_prefix(group->bgp, prefix, group->conf->password);
9e7d9a61 2860
d62a17ae 2861 return 0;
718e3744 2862}
6b0655a2 2863
d62a17ae 2864int peer_group_listen_range_del(struct peer_group *group, struct prefix *range)
718e3744 2865{
d62a17ae 2866 struct prefix *prefix, prefix2;
2867 struct listnode *node, *nnode;
2868 struct peer *peer;
2869 afi_t afi;
718e3744 2870
d62a17ae 2871 afi = family2afi(range->family);
1ff9a340 2872
d62a17ae 2873 /* Identify the listen range. */
2874 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode, prefix)) {
2875 if (prefix_same(range, prefix))
2876 break;
1ff9a340 2877 }
718e3744 2878
d62a17ae 2879 if (!prefix)
2880 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND;
2881
d62a17ae 2882 /* Dispose off any dynamic neighbors that exist due to this listen range
2883 */
2884 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2885 if (!peer_dynamic_neighbor(peer))
2886 continue;
2887
0154d8ce
DS
2888 if (sockunion2hostprefix(&peer->su, &prefix2)
2889 && prefix_match(prefix, &prefix2)) {
d62a17ae 2890 if (bgp_debug_neighbor_events(peer))
2891 zlog_debug(
2dbe669b
DA
2892 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
2893 peer->host, group->name, prefix);
d62a17ae 2894 peer_delete(peer);
2895 }
2896 }
718e3744 2897
d62a17ae 2898 /* Get rid of the listen range */
2899 listnode_delete(group->listen_range[afi], prefix);
718e3744 2900
9e7d9a61
QY
2901 /* Remove passwords for deleted ranges */
2902 if (group->conf->password)
a4faae3a 2903 bgp_md5_unset_prefix(group->bgp, prefix);
9e7d9a61 2904
d62a17ae 2905 return 0;
2906}
e0701b79 2907
d62a17ae 2908/* Bind specified peer to peer group. */
2909int peer_group_bind(struct bgp *bgp, union sockunion *su, struct peer *peer,
2910 struct peer_group *group, as_t *as)
2911{
2912 int first_member = 0;
2913 afi_t afi;
2914 safi_t safi;
3061acc2 2915 enum bgp_peer_sort ptype, gtype;
d62a17ae 2916
2917 /* Lookup the peer. */
2918 if (!peer)
2919 peer = peer_lookup(bgp, su);
2920
2921 /* The peer exist, bind it to the peer-group */
2922 if (peer) {
ed9fb826 2923 /* When the peer already belongs to a peer-group, check the
d62a17ae 2924 * consistency. */
ed9fb826
DW
2925 if (peer_group_active(peer)) {
2926
2927 /* The peer is already bound to the peer-group,
d8a079e8
DW
2928 * nothing to do
2929 */
ed9fb826
DW
2930 if (strcmp(peer->group->name, group->name) == 0)
2931 return 0;
2932 else
2933 return BGP_ERR_PEER_GROUP_CANT_CHANGE;
2934 }
d62a17ae 2935
2936 /* The peer has not specified a remote-as, inherit it from the
2937 * peer-group */
2938 if (peer->as_type == AS_UNSPECIFIED) {
2939 peer->as_type = group->conf->as_type;
2940 peer->as = group->conf->as;
faa16034 2941 peer->sort = group->conf->sort;
d62a17ae 2942 }
718e3744 2943
469cbb0f
DS
2944 ptype = peer_sort(peer);
2945 if (!group->conf->as && ptype != BGP_PEER_UNSPECIFIED) {
2946 gtype = peer_sort(group->conf);
2947 if ((gtype != BGP_PEER_INTERNAL) && (gtype != ptype)) {
d62a17ae 2948 if (as)
2949 *as = peer->as;
2950 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
2951 }
718e3744 2952
469cbb0f 2953 if (gtype == BGP_PEER_INTERNAL)
d62a17ae 2954 first_member = 1;
2955 }
718e3744 2956
d62a17ae 2957 peer_group2peer_config_copy(group, peer);
2958
05c7a1cc
QY
2959 FOREACH_AFI_SAFI (afi, safi) {
2960 if (group->conf->afc[afi][safi]) {
2961 peer->afc[afi][safi] = 1;
2962
2963 if (peer_af_find(peer, afi, safi)
2964 || peer_af_create(peer, afi, safi)) {
2965 peer_group2peer_config_copy_af(
2966 group, peer, afi, safi);
2967 }
2968 } else if (peer->afc[afi][safi])
2969 peer_deactivate(peer, afi, safi);
a4d82a8a 2970 }
d62a17ae 2971
2972 if (peer->group) {
2973 assert(group && peer->group == group);
2974 } else {
0866cdaf
A
2975 listnode_delete(bgp->peer, peer);
2976
d62a17ae 2977 peer->group = group;
2978 listnode_add_sort(bgp->peer, peer);
2979
2980 peer = peer_lock(peer); /* group->peer list reference */
2981 listnode_add(group->peer, peer);
2982 }
718e3744 2983
d62a17ae 2984 if (first_member) {
469cbb0f 2985 gtype = peer_sort(group->conf);
d62a17ae 2986 /* Advertisement-interval reset */
b90a8e13
PM
2987 if (!CHECK_FLAG(group->conf->flags,
2988 PEER_FLAG_ROUTEADV)) {
2989 group->conf->v_routeadv =
469cbb0f 2990 (gtype == BGP_PEER_IBGP)
b90a8e13
PM
2991 ? BGP_DEFAULT_IBGP_ROUTEADV
2992 : BGP_DEFAULT_EBGP_ROUTEADV;
d62a17ae 2993 }
2994
2995 /* ebgp-multihop reset */
469cbb0f 2996 if (gtype == BGP_PEER_IBGP)
d62a17ae 2997 group->conf->ttl = MAXTTL;
2998
2999 /* local-as reset */
469cbb0f 3000 if (gtype != BGP_PEER_EBGP) {
d62a17ae 3001 group->conf->change_local_as = 0;
a14810f4
PM
3002 peer_flag_unset(group->conf,
3003 PEER_FLAG_LOCAL_AS);
3004 peer_flag_unset(group->conf,
3005 PEER_FLAG_LOCAL_AS_NO_PREPEND);
3006 peer_flag_unset(group->conf,
3007 PEER_FLAG_LOCAL_AS_REPLACE_AS);
d62a17ae 3008 }
3009 }
718e3744 3010
d62a17ae 3011 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
718e3744 3012
d62a17ae 3013 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
3014 peer->last_reset = PEER_DOWN_RMAP_BIND;
3015 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
3016 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
3017 } else {
3018 bgp_session_reset(peer);
3019 }
3020 }
718e3744 3021
d62a17ae 3022 /* Create a new peer. */
3023 else {
3024 if ((group->conf->as_type == AS_SPECIFIED)
3025 && (!group->conf->as)) {
3026 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS;
3027 }
718e3744 3028
d62a17ae 3029 peer = peer_create(su, NULL, bgp, bgp->as, group->conf->as,
e84c59af 3030 group->conf->as_type, group);
718e3744 3031
d62a17ae 3032 peer = peer_lock(peer); /* group->peer list reference */
3033 listnode_add(group->peer, peer);
718e3744 3034
d62a17ae 3035 peer_group2peer_config_copy(group, peer);
a4ed4829 3036
d62a17ae 3037 /* If the peer-group is active for this afi/safi then activate
3038 * for this peer */
05c7a1cc
QY
3039 FOREACH_AFI_SAFI (afi, safi) {
3040 if (group->conf->afc[afi][safi]) {
3041 peer->afc[afi][safi] = 1;
e84c59af
DA
3042
3043 if (!peer_af_find(peer, afi, safi))
3044 peer_af_create(peer, afi, safi);
3045
05c7a1cc
QY
3046 peer_group2peer_config_copy_af(group, peer, afi,
3047 safi);
3048 } else if (peer->afc[afi][safi])
3049 peer_deactivate(peer, afi, safi);
3050 }
718e3744 3051
d62a17ae 3052 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
718e3744 3053
d62a17ae 3054 /* Set up peer's events and timers. */
3055 if (peer_active(peer))
3056 bgp_timer_set(peer);
3057 }
718e3744 3058
d62a17ae 3059 return 0;
718e3744 3060}
3061
cc9f21da 3062static void bgp_startup_timer_expire(struct thread *thread)
d62a17ae 3063{
3064 struct bgp *bgp;
718e3744 3065
d62a17ae 3066 bgp = THREAD_ARG(thread);
3067 bgp->t_startup = NULL;
d62a17ae 3068}
e0701b79 3069
829e590b
DS
3070/*
3071 * On shutdown we call the cleanup function which
3072 * does a free of the link list nodes, free up
3073 * the data we are pointing at too.
3074 */
3075static void bgp_vrf_string_name_delete(void *data)
3076{
3077 char *vname = data;
3078
3079 XFREE(MTYPE_TMP, vname);
3080}
3081
d62a17ae 3082/* BGP instance creation by `router bgp' commands. */
3083static struct bgp *bgp_create(as_t *as, const char *name,
3084 enum bgp_instance_type inst_type)
3085{
0291c246
MK
3086 struct bgp *bgp;
3087 afi_t afi;
3088 safi_t safi;
d62a17ae 3089
4953391b 3090 bgp = XCALLOC(MTYPE_BGP, sizeof(struct bgp));
d62a17ae 3091
3092 if (BGP_DEBUG(zebra, ZEBRA)) {
3093 if (inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3094 zlog_debug("Creating Default VRF, AS %u", *as);
3095 else
3096 zlog_debug("Creating %s %s, AS %u",
3097 (inst_type == BGP_INSTANCE_TYPE_VRF)
3098 ? "VRF"
3099 : "VIEW",
3100 name, *as);
718e3744 3101 }
adbac85e 3102
e2f3a930
T
3103 /* Default the EVPN VRF to the default one */
3104 if (inst_type == BGP_INSTANCE_TYPE_DEFAULT && !bgp_master.bgp_evpn) {
3105 bgp_lock(bgp);
3106 bm->bgp_evpn = bgp;
3107 }
3108
d62a17ae 3109 bgp_lock(bgp);
b6c386bb
DS
3110
3111 bgp_process_queue_init(bgp);
9df5ac49 3112 bgp->heuristic_coalesce = true;
d62a17ae 3113 bgp->inst_type = inst_type;
3114 bgp->vrf_id = (inst_type == BGP_INSTANCE_TYPE_DEFAULT) ? VRF_DEFAULT
3115 : VRF_UNKNOWN;
3116 bgp->peer_self = peer_new(bgp);
0a22ddfb 3117 XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->host);
d62a17ae 3118 bgp->peer_self->host =
3119 XSTRDUP(MTYPE_BGP_PEER_HOST, "Static announcement");
e1b36e13 3120 XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->hostname);
6b3ee3a0 3121 if (cmd_hostname_get())
60466a63
QY
3122 bgp->peer_self->hostname =
3123 XSTRDUP(MTYPE_BGP_PEER_HOST, cmd_hostname_get());
d77114b7 3124
e1b36e13 3125 XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->domainname);
6b3ee3a0 3126 if (cmd_domainname_get())
60466a63
QY
3127 bgp->peer_self->domainname =
3128 XSTRDUP(MTYPE_BGP_PEER_HOST, cmd_domainname_get());
d62a17ae 3129 bgp->peer = list_new();
3130 bgp->peer->cmp = (int (*)(void *, void *))peer_cmp;
a4d82a8a 3131 bgp->peerhash = hash_create(peer_hash_key_make, peer_hash_same,
3f65c5b1 3132 "BGP Peer Hash");
40520c36 3133 bgp->peerhash->max_size = BGP_PEER_MAX_HASH_SIZE;
d62a17ae 3134
3135 bgp->group = list_new();
3136 bgp->group->cmp = (int (*)(void *, void *))peer_group_cmp;
3137
05c7a1cc 3138 FOREACH_AFI_SAFI (afi, safi) {
960035b2
PZ
3139 bgp->route[afi][safi] = bgp_table_init(bgp, afi, safi);
3140 bgp->aggregate[afi][safi] = bgp_table_init(bgp, afi, safi);
3141 bgp->rib[afi][safi] = bgp_table_init(bgp, afi, safi);
05c7a1cc
QY
3142
3143 /* Enable maximum-paths */
3144 bgp_maximum_paths_set(bgp, afi, safi, BGP_PEER_EBGP,
3145 multipath_num, 0);
3146 bgp_maximum_paths_set(bgp, afi, safi, BGP_PEER_IBGP,
3147 multipath_num, 0);
cfd47646 3148 /* Initialize graceful restart info */
3149 bgp->gr_info[afi][safi].eor_required = 0;
3150 bgp->gr_info[afi][safi].eor_received = 0;
3151 bgp->gr_info[afi][safi].t_select_deferral = NULL;
3152 bgp->gr_info[afi][safi].t_route_select = NULL;
26742171 3153 bgp->gr_info[afi][safi].gr_deferred = 0;
05c7a1cc 3154 }
d62a17ae 3155
d70583f7
D
3156 bgp->v_update_delay = bm->v_update_delay;
3157 bgp->v_establish_wait = bm->v_establish_wait;
d62a17ae 3158 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
3159 bgp->default_subgroup_pkt_queue_max =
3160 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX;
5d5393b9 3161 bgp_timers_unset(bgp);
b042667a 3162 bgp->default_min_holdtime = 0;
d62a17ae 3163 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
3164 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
cfd47646 3165 bgp->select_defer_time = BGP_DEFAULT_SELECT_DEFERRAL_TIME;
dc95985f 3166 bgp->rib_stale_time = BGP_DEFAULT_RIB_STALE_TIME;
d62a17ae 3167 bgp->dynamic_neighbors_limit = BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT;
3168 bgp->dynamic_neighbors_count = 0;
b1875e65 3169 bgp->lb_ref_bw = BGP_LINK_BW_REF_BW;
f7e1c681 3170 bgp->lb_handling = BGP_LINK_BW_ECMP;
7f972cd8 3171 bgp->reject_as_sets = false;
389e4f92 3172 bgp->condition_check_period = DEFAULT_CONDITIONAL_ROUTES_POLL_TIME;
dcc68b5e 3173 bgp_addpath_init_bgp_data(&bgp->tx_addpath);
f852eb98 3174 bgp->fast_convergence = false;
d62a17ae 3175 bgp->as = *as;
8606be87 3176 bgp->llgr_stale_time = BGP_DEFAULT_LLGR_STALE_TIME;
d62a17ae 3177
49e5a4a0 3178#ifdef ENABLE_BGP_VNC
d62a17ae 3179 if (inst_type != BGP_INSTANCE_TYPE_VRF) {
3180 bgp->rfapi = bgp_rfapi_new(bgp);
3181 assert(bgp->rfapi);
3182 assert(bgp->rfapi_cfg);
3183 }
3184#endif /* ENABLE_BGP_VNC */
adbac85e 3185
ddb5b488 3186 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
e70e9f8e
PZ
3187 bgp->vpn_policy[afi].bgp = bgp;
3188 bgp->vpn_policy[afi].afi = afi;
ddb5b488
PZ
3189 bgp->vpn_policy[afi].tovpn_label = MPLS_LABEL_NONE;
3190 bgp->vpn_policy[afi].tovpn_zebra_vrf_label_last_sent =
3191 MPLS_LABEL_NONE;
12a844a5
DS
3192
3193 bgp->vpn_policy[afi].import_vrf = list_new();
829e590b
DS
3194 bgp->vpn_policy[afi].import_vrf->del =
3195 bgp_vrf_string_name_delete;
12a844a5 3196 bgp->vpn_policy[afi].export_vrf = list_new();
829e590b
DS
3197 bgp->vpn_policy[afi].export_vrf->del =
3198 bgp_vrf_string_name_delete;
ddb5b488 3199 }
dc95985f 3200 if (name)
d62a17ae 3201 bgp->name = XSTRDUP(MTYPE_BGP, name);
dc95985f 3202
3203 thread_add_timer(bm->master, bgp_startup_timer_expire, bgp,
36235319 3204 bgp->restart_time, &bgp->t_startup);
06370dac 3205
3572fb26
PZ
3206 /* printable name we can use in debug messages */
3207 if (inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
3208 bgp->name_pretty = XSTRDUP(MTYPE_BGP, "VRF default");
3209 } else {
3210 const char *n;
3211 int len;
3212
3213 if (bgp->name)
3214 n = bgp->name;
3215 else
3216 n = "?";
3217
3218 len = 4 + 1 + strlen(n) + 1; /* "view foo\0" */
3219
3220 bgp->name_pretty = XCALLOC(MTYPE_BGP, len);
3221 snprintf(bgp->name_pretty, len, "%s %s",
3222 (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
3223 ? "VRF"
3224 : "VIEW",
3225 n);
3226 }
3227
555e09d4
QY
3228 atomic_store_explicit(&bgp->wpkt_quanta, BGP_WRITE_PACKET_MAX,
3229 memory_order_relaxed);
3230 atomic_store_explicit(&bgp->rpkt_quanta, BGP_READ_PACKET_MAX,
3231 memory_order_relaxed);
d62a17ae 3232 bgp->coalesce_time = BGP_DEFAULT_SUBGROUP_COALESCE_TIME;
b16bcbba 3233 bgp->default_af[AFI_IP][SAFI_UNICAST] = true;
adbac85e 3234
d62a17ae 3235 QOBJ_REG(bgp, bgp);
adbac85e 3236
d62a17ae 3237 update_bgp_group_init(bgp);
92708db6 3238
3239 /* assign a unique rd id for auto derivation of vrf's RD */
3240 bf_assign_index(bm->rd_idspace, bgp->vrf_rd_id);
3241
85c8d83b
CS
3242 bgp->evpn_info = XCALLOC(MTYPE_BGP_EVPN_INFO,
3243 sizeof(struct bgp_evpn_info));
3244
d62a17ae 3245 bgp_evpn_init(bgp);
c589d847 3246 bgp_evpn_vrf_es_init(bgp);
f3d32faa 3247 bgp_pbr_init(bgp);
92a9e6f2 3248 bgp_srv6_init(bgp);
794b37d5 3249
3250 /*initilize global GR FSM */
3251 bgp_global_gr_init(bgp);
b17826b7
DS
3252
3253 memset(&bgp->ebgprequirespolicywarning, 0,
3254 sizeof(bgp->ebgprequirespolicywarning));
3255
d62a17ae 3256 return bgp;
718e3744 3257}
3258
d62a17ae 3259/* Return the "default VRF" instance of BGP. */
3260struct bgp *bgp_get_default(void)
718e3744 3261{
d62a17ae 3262 struct bgp *bgp;
3263 struct listnode *node, *nnode;
3264
3265 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3266 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3267 return bgp;
3268 return NULL;
718e3744 3269}
3270
d62a17ae 3271/* Lookup BGP entry. */
3272struct bgp *bgp_lookup(as_t as, const char *name)
718e3744 3273{
d62a17ae 3274 struct bgp *bgp;
3275 struct listnode *node, *nnode;
6b0655a2 3276
d62a17ae 3277 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3278 if (bgp->as == as
3279 && ((bgp->name == NULL && name == NULL)
3280 || (bgp->name && name && strcmp(bgp->name, name) == 0)))
3281 return bgp;
3282 return NULL;
3283}
73d70fa6 3284
d62a17ae 3285/* Lookup BGP structure by view name. */
3286struct bgp *bgp_lookup_by_name(const char *name)
73d70fa6 3287{
d62a17ae 3288 struct bgp *bgp;
3289 struct listnode *node, *nnode;
3290
3291 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3292 if ((bgp->name == NULL && name == NULL)
3293 || (bgp->name && name && strcmp(bgp->name, name) == 0))
3294 return bgp;
3295 return NULL;
73d70fa6
DL
3296}
3297
d62a17ae 3298/* Lookup BGP instance based on VRF id. */
3299/* Note: Only to be used for incoming messages from Zebra. */
3300struct bgp *bgp_lookup_by_vrf_id(vrf_id_t vrf_id)
73d70fa6 3301{
d62a17ae 3302 struct vrf *vrf;
3303
3304 /* Lookup VRF (in tree) and follow link. */
3305 vrf = vrf_lookup_by_id(vrf_id);
3306 if (!vrf)
3307 return NULL;
3308 return (vrf->info) ? (struct bgp *)vrf->info : NULL;
73d70fa6
DL
3309}
3310
e2f3a930
T
3311/* Sets the BGP instance where EVPN is enabled */
3312void bgp_set_evpn(struct bgp *bgp)
3313{
3314 if (bm->bgp_evpn == bgp)
3315 return;
3316
3317 /* First, release the reference count we hold on the instance */
3318 if (bm->bgp_evpn)
3319 bgp_unlock(bm->bgp_evpn);
3320
3321 bm->bgp_evpn = bgp;
3322
3323 /* Increase the reference count on this new VRF */
3324 if (bm->bgp_evpn)
3325 bgp_lock(bm->bgp_evpn);
3326}
3327
3328/* Returns the BGP instance where EVPN is enabled, if any */
3329struct bgp *bgp_get_evpn(void)
3330{
3331 return bm->bgp_evpn;
3332}
3333
e5619c28
PG
3334/* handle socket creation or deletion, if necessary
3335 * this is called for all new BGP instances
3336 */
996c9314
LB
3337int bgp_handle_socket(struct bgp *bgp, struct vrf *vrf, vrf_id_t old_vrf_id,
3338 bool create)
e5619c28 3339{
85e9cd9a
AMR
3340 struct listnode *node;
3341 char *address;
e5619c28
PG
3342
3343 /* Create BGP server socket, if listen mode not disabled */
3344 if (!bgp || bgp_option_check(BGP_OPT_NO_LISTEN))
3345 return 0;
3ad00234 3346 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
e5619c28
PG
3347 /*
3348 * suppress vrf socket
3349 */
a8f58eb6 3350 if (!create) {
3ad00234
LB
3351 bgp_close_vrf_socket(bgp);
3352 return 0;
e5619c28 3353 }
3ad00234
LB
3354 if (vrf == NULL)
3355 return BGP_ERR_INVALID_VALUE;
e5619c28
PG
3356 /* do nothing
3357 * if vrf_id did not change
3358 */
3359 if (vrf->vrf_id == old_vrf_id)
3360 return 0;
3361 if (old_vrf_id != VRF_UNKNOWN) {
3362 /* look for old socket. close it. */
3363 bgp_close_vrf_socket(bgp);
3364 }
3365 /* if backend is not yet identified ( VRF_UNKNOWN) then
3366 * creation will be done later
3367 */
3368 if (vrf->vrf_id == VRF_UNKNOWN)
3369 return 0;
85e9cd9a
AMR
3370 if (list_isempty(bm->addresses)) {
3371 if (bgp_socket(bgp, bm->port, NULL) < 0)
3372 return BGP_ERR_INVALID_VALUE;
3373 } else {
3374 for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
3375 if (bgp_socket(bgp, bm->port, address) < 0)
3376 return BGP_ERR_INVALID_VALUE;
3377 }
3ad00234
LB
3378 return 0;
3379 } else
3380 return bgp_check_main_socket(create, bgp);
e5619c28
PG
3381}
3382
bdaadb91
CS
3383int bgp_lookup_by_as_name_type(struct bgp **bgp_val, as_t *as, const char *name,
3384 enum bgp_instance_type inst_type)
d62a17ae 3385{
3386 struct bgp *bgp;
3387
3388 /* Multiple instance check. */
8fbb4853
DS
3389 if (name)
3390 bgp = bgp_lookup_by_name(name);
3391 else
d62a17ae 3392 bgp = bgp_get_default();
3393
8fbb4853 3394 if (bgp) {
0d6d0208 3395 *bgp_val = bgp;
8fbb4853
DS
3396 if (bgp->as != *as) {
3397 *as = bgp->as;
d09328e5 3398 return BGP_ERR_AS_MISMATCH;
d62a17ae 3399 }
8fbb4853
DS
3400 if (bgp->inst_type != inst_type)
3401 return BGP_ERR_INSTANCE_MISMATCH;
d5568431 3402 return BGP_SUCCESS;
d62a17ae 3403 }
bdaadb91
CS
3404 *bgp_val = NULL;
3405
3406 return BGP_SUCCESS;
3407}
3408
3409/* Called from VTY commands. */
3410int bgp_get(struct bgp **bgp_val, as_t *as, const char *name,
3411 enum bgp_instance_type inst_type)
3412{
3413 struct bgp *bgp;
3414 struct vrf *vrf = NULL;
3415 int ret = 0;
3416
3417 ret = bgp_lookup_by_as_name_type(bgp_val, as, name, inst_type);
d09328e5 3418 if (ret || *bgp_val)
bdaadb91 3419 return ret;
73d70fa6 3420
d62a17ae 3421 bgp = bgp_create(as, name, inst_type);
e7682ccd
DS
3422
3423 /*
3424 * view instances will never work inside of a vrf
3425 * as such they must always be in the VRF_DEFAULT
3426 * Also we must set this to something useful because
3427 * of the vrf socket code needing an actual useful
3428 * default value to send to the underlying OS.
3429 *
3430 * This code is currently ignoring vrf based
3431 * code using the -Z option( and that is probably
3432 * best addressed elsewhere in the code )
3433 */
3434 if (inst_type == BGP_INSTANCE_TYPE_VIEW)
3435 bgp->vrf_id = VRF_DEFAULT;
3436
2951a7a4 3437 bgp_router_id_set(bgp, &bgp->router_id_zebra, true);
d62a17ae 3438 bgp_address_init(bgp);
db0e1937 3439 bgp_tip_hash_init(bgp);
d62a17ae 3440 bgp_scan_init(bgp);
3441 *bgp_val = bgp;
eb821189 3442
d62a17ae 3443 bgp->t_rmap_def_originate_eval = NULL;
e5cc509c 3444
d62a17ae 3445 /* If Default instance or VRF, link to the VRF structure, if present. */
3446 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
3447 || bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
d62a17ae 3448 vrf = bgp_vrf_lookup_by_instance_type(bgp);
3449 if (vrf)
3450 bgp_vrf_link(bgp, vrf);
3451 }
e5619c28
PG
3452 /* BGP server socket already processed if BGP instance
3453 * already part of the list
3454 */
3455 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, true);
3456 listnode_add(bm->bgp, bgp);
718e3744 3457
bb4ef1ae
DS
3458 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
3459 if (BGP_DEBUG(zebra, ZEBRA))
3460 zlog_debug("%s: Registering BGP instance %s to zebra",
15569c58 3461 __func__, name);
d62a17ae 3462 bgp_zebra_instance_register(bgp);
bb4ef1ae 3463 }
718e3744 3464
5d5393b9 3465 return BGP_CREATED;
718e3744 3466}
6b0655a2 3467
6cc0114b
IR
3468static void bgp_zclient_set_redist(afi_t afi, int type, unsigned short instance,
3469 vrf_id_t vrf_id, bool set)
3470{
3471 if (instance) {
3472 if (set)
3473 redist_add_instance(&zclient->mi_redist[afi][type],
3474 instance);
3475 else
3476 redist_del_instance(&zclient->mi_redist[afi][type],
3477 instance);
3478 } else {
3479 if (set)
3480 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
3481 else
3482 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
3483 }
3484}
3485
3486static void bgp_set_redist_vrf_bitmaps(struct bgp *bgp, bool set)
3487{
3488 afi_t afi;
3489 int i;
3490 struct list *red_list;
3491 struct listnode *node;
3492 struct bgp_redist *red;
3493
3494 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
3495 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
3496
3497 red_list = bgp->redist[afi][i];
3498 if (!red_list)
3499 continue;
3500
3501 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
3502 bgp_zclient_set_redist(afi, i, red->instance,
3503 bgp->vrf_id, set);
3504 }
3505 }
3506}
3507
d62a17ae 3508/*
3509 * Make BGP instance "up". Applies only to VRFs (non-default) and
3510 * implies the VRF has been learnt from Zebra.
3511 */
3512void bgp_instance_up(struct bgp *bgp)
718e3744 3513{
d62a17ae 3514 struct peer *peer;
3515 struct listnode *node, *next;
718e3744 3516
6cc0114b
IR
3517 bgp_set_redist_vrf_bitmaps(bgp, true);
3518
d62a17ae 3519 /* Register with zebra. */
3520 bgp_zebra_instance_register(bgp);
718e3744 3521
d62a17ae 3522 /* Kick off any peers that may have been configured. */
3523 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer)) {
3524 if (!BGP_PEER_START_SUPPRESSED(peer))
3525 BGP_EVENT_ADD(peer, BGP_Start);
718e3744 3526 }
3527
d62a17ae 3528 /* Process any networks that have been configured. */
3529 bgp_static_add(bgp);
3530}
3531
3532/*
3533 * Make BGP instance "down". Applies only to VRFs (non-default) and
3534 * implies the VRF has been deleted by Zebra.
3535 */
3536void bgp_instance_down(struct bgp *bgp)
3537{
3538 struct peer *peer;
3539 struct listnode *node;
3540 struct listnode *next;
3541
3542 /* Stop timers. */
3543 if (bgp->t_rmap_def_originate_eval) {
3544 BGP_TIMER_OFF(bgp->t_rmap_def_originate_eval);
3545 bgp_unlock(bgp); /* TODO - This timer is started with a lock -
3546 why? */
718e3744 3547 }
3548
d62a17ae 3549 /* Bring down peers, so corresponding routes are purged. */
3550 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer)) {
3551 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
3552 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
3553 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
3554 else
3555 bgp_session_reset(peer);
3556 }
718e3744 3557
d62a17ae 3558 /* Purge network and redistributed routes. */
3559 bgp_purge_static_redist_routes(bgp);
ee7ca6c0 3560
3561 /* Cleanup registered nexthops (flags) */
3562 bgp_cleanup_nexthops(bgp);
6cc0114b
IR
3563
3564 bgp_zebra_instance_deregister(bgp);
3565
3566 bgp_set_redist_vrf_bitmaps(bgp, false);
718e3744 3567}
3568
d62a17ae 3569/* Delete BGP instance. */
3570int bgp_delete(struct bgp *bgp)
3571{
3572 struct peer *peer;
3573 struct peer_group *group;
3574 struct listnode *node, *next;
3575 struct vrf *vrf;
3576 afi_t afi;
cfd47646 3577 safi_t safi;
d62a17ae 3578 int i;
cfd47646 3579 struct graceful_restart_info *gr_info;
d62a17ae 3580
bf383464 3581 assert(bgp);
2b9bcf30 3582
46aeabed
LS
3583 bgp_soft_reconfig_table_task_cancel(bgp, NULL, NULL);
3584
1bfd9dcc
PR
3585 /* make sure we withdraw any exported routes */
3586 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP, bgp_get_default(),
3587 bgp);
3588 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP6, bgp_get_default(),
3589 bgp);
3590
3591 bgp_vpn_leak_unimport(bgp);
3592
2b9bcf30
DL
3593 hook_call(bgp_inst_delete, bgp);
3594
d62a17ae 3595 THREAD_OFF(bgp->t_startup);
c969b2ed
DS
3596 THREAD_OFF(bgp->t_maxmed_onstartup);
3597 THREAD_OFF(bgp->t_update_delay);
3598 THREAD_OFF(bgp->t_establish_wait);
d62a17ae 3599
9f822fa2 3600 /* Set flag indicating bgp instance delete in progress */
892fedb6 3601 SET_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS);
9f822fa2 3602
cfd47646 3603 /* Delete the graceful restart info */
3604 FOREACH_AFI_SAFI (afi, safi) {
56c226e7
DS
3605 struct thread *t;
3606
cfd47646 3607 gr_info = &bgp->gr_info[afi][safi];
13909c4f
DS
3608 if (!gr_info)
3609 continue;
3610
3611 BGP_TIMER_OFF(gr_info->t_select_deferral);
56c226e7
DS
3612
3613 t = gr_info->t_route_select;
3614 if (t) {
3615 void *info = THREAD_ARG(t);
3616
3617 XFREE(MTYPE_TMP, info);
3618 }
13909c4f 3619 BGP_TIMER_OFF(gr_info->t_route_select);
cfd47646 3620 }
3621
d62a17ae 3622 if (BGP_DEBUG(zebra, ZEBRA)) {
3623 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3624 zlog_debug("Deleting Default VRF");
3625 else
3626 zlog_debug("Deleting %s %s",
3627 (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
3628 ? "VRF"
3629 : "VIEW",
3630 bgp->name);
718e3744 3631 }
3632
10ebe1ab
MK
3633 /* unmap from RT list */
3634 bgp_evpn_vrf_delete(bgp);
3635
dfd17258
PG
3636 /* unmap bgp vrf label */
3637 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP);
3638 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP6);
3639
d62a17ae 3640 /* Stop timers. */
3641 if (bgp->t_rmap_def_originate_eval) {
3642 BGP_TIMER_OFF(bgp->t_rmap_def_originate_eval);
3643 bgp_unlock(bgp); /* TODO - This timer is started with a lock -
3644 why? */
718e3744 3645 }
3646
d62a17ae 3647 /* Inform peers we're going down. */
3648 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer)) {
3649 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
3650 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
3651 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
718e3744 3652 }
3653
d62a17ae 3654 /* Delete static routes (networks). */
3655 bgp_static_delete(bgp);
6b0655a2 3656
d62a17ae 3657 /* Unset redistribution. */
3658 for (afi = AFI_IP; afi < AFI_MAX; afi++)
3659 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
3660 if (i != ZEBRA_ROUTE_BGP)
3661 bgp_redistribute_unset(bgp, afi, i, 0);
718e3744 3662
d62a17ae 3663 /* Free peers and peer-groups. */
3664 for (ALL_LIST_ELEMENTS(bgp->group, node, next, group))
3665 peer_group_delete(group);
718e3744 3666
d62a17ae 3667 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer))
3668 peer_delete(peer);
3669
3670 if (bgp->peer_self) {
3671 peer_delete(bgp->peer_self);
3672 bgp->peer_self = NULL;
718e3744 3673 }
3674
d62a17ae 3675 update_bgp_group_free(bgp);
718e3744 3676
d62a17ae 3677/* TODO - Other memory may need to be freed - e.g., NHT */
3678
49e5a4a0 3679#ifdef ENABLE_BGP_VNC
d62a17ae 3680 rfapi_delete(bgp);
3681#endif
3682 bgp_cleanup_routes(bgp);
718e3744 3683
301ad80a
PG
3684 for (afi = 0; afi < AFI_MAX; ++afi) {
3685 if (!bgp->vpn_policy[afi].import_redirect_rtlist)
3686 continue;
3687 ecommunity_free(
3688 &bgp->vpn_policy[afi]
3689 .import_redirect_rtlist);
3690 bgp->vpn_policy[afi].import_redirect_rtlist = NULL;
3691 }
718e3744 3692
d62a17ae 3693 /* Deregister from Zebra, if needed */
bb4ef1ae
DS
3694 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
3695 if (BGP_DEBUG(zebra, ZEBRA))
5e81f5dd
DS
3696 zlog_debug(
3697 "%s: deregistering this bgp %s instance from zebra",
3698 __func__, bgp->name);
d62a17ae 3699 bgp_zebra_instance_deregister(bgp);
bb4ef1ae 3700 }
718e3744 3701
de6058ab
AK
3702 /* Remove visibility via the master list - there may however still be
3703 * routes to be processed still referencing the struct bgp.
3704 */
3705 listnode_delete(bm->bgp, bgp);
3706
d62a17ae 3707 /* Free interfaces in this instance. */
3708 bgp_if_finish(bgp);
718e3744 3709
d62a17ae 3710 vrf = bgp_vrf_lookup_by_instance_type(bgp);
e5619c28 3711 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, false);
d62a17ae 3712 if (vrf)
3713 bgp_vrf_unlink(bgp, vrf);
718e3744 3714
e2f3a930
T
3715 /* Update EVPN VRF pointer */
3716 if (bm->bgp_evpn == bgp) {
3717 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3718 bgp_set_evpn(NULL);
3719 else
3720 bgp_set_evpn(bgp_get_default());
3721 }
3722
b6c386bb
DS
3723 if (bgp->process_queue)
3724 work_queue_free_and_null(&bgp->process_queue);
3725
d62a17ae 3726 thread_master_free_unused(bm->master);
3727 bgp_unlock(bgp); /* initial reference */
718e3744 3728
d62a17ae 3729 return 0;
718e3744 3730}
6b0655a2 3731
92375c91 3732void bgp_free(struct bgp *bgp)
d93f7ffc 3733{
d62a17ae 3734 afi_t afi;
3735 safi_t safi;
3736 struct bgp_table *table;
9bcb3eef 3737 struct bgp_dest *dest;
f8265580 3738 struct bgp_rmap *rmap;
d93f7ffc 3739
d62a17ae 3740 QOBJ_UNREG(bgp);
d93f7ffc 3741
6a154c88
DL
3742 list_delete(&bgp->group);
3743 list_delete(&bgp->peer);
966f821c 3744
d62a17ae 3745 if (bgp->peerhash) {
3746 hash_free(bgp->peerhash);
3747 bgp->peerhash = NULL;
3748 }
718e3744 3749
05c7a1cc
QY
3750 FOREACH_AFI_SAFI (afi, safi) {
3751 /* Special handling for 2-level routing tables. */
3752 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
3753 || safi == SAFI_EVPN) {
9bcb3eef
DS
3754 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
3755 dest = bgp_route_next(dest)) {
3756 table = bgp_dest_get_bgp_table_info(dest);
05c7a1cc 3757 bgp_table_finish(&table);
d62a17ae 3758 }
d62a17ae 3759 }
05c7a1cc
QY
3760 if (bgp->route[afi][safi])
3761 bgp_table_finish(&bgp->route[afi][safi]);
3762 if (bgp->aggregate[afi][safi])
3763 bgp_table_finish(&bgp->aggregate[afi][safi]);
3764 if (bgp->rib[afi][safi])
3765 bgp_table_finish(&bgp->rib[afi][safi]);
3766 rmap = &bgp->table_map[afi][safi];
0a22ddfb 3767 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
05c7a1cc 3768 }
718e3744 3769
d62a17ae 3770 bgp_scan_finish(bgp);
3771 bgp_address_destroy(bgp);
db0e1937 3772 bgp_tip_hash_destroy(bgp);
966f821c 3773
92708db6 3774 /* release the auto RD id */
3775 bf_release_index(bm->rd_idspace, bgp->vrf_rd_id);
3776
d62a17ae 3777 bgp_evpn_cleanup(bgp);
a6b07429 3778 bgp_pbr_cleanup(bgp);
92a9e6f2 3779 bgp_srv6_cleanup(bgp);
85c8d83b 3780 XFREE(MTYPE_BGP_EVPN_INFO, bgp->evpn_info);
829e590b
DS
3781
3782 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
9c2fd3fe 3783 enum vpn_policy_direction dir;
829e590b
DS
3784
3785 if (bgp->vpn_policy[afi].import_vrf)
6a154c88 3786 list_delete(&bgp->vpn_policy[afi].import_vrf);
829e590b 3787 if (bgp->vpn_policy[afi].export_vrf)
6a154c88 3788 list_delete(&bgp->vpn_policy[afi].export_vrf);
829e590b
DS
3789
3790 dir = BGP_VPN_POLICY_DIR_FROMVPN;
3791 if (bgp->vpn_policy[afi].rtlist[dir])
3792 ecommunity_free(&bgp->vpn_policy[afi].rtlist[dir]);
3793 dir = BGP_VPN_POLICY_DIR_TOVPN;
3794 if (bgp->vpn_policy[afi].rtlist[dir])
3795 ecommunity_free(&bgp->vpn_policy[afi].rtlist[dir]);
3796 }
3797
0a22ddfb
QY
3798 XFREE(MTYPE_BGP, bgp->name);
3799 XFREE(MTYPE_BGP, bgp->name_pretty);
0d020cd6 3800 XFREE(MTYPE_BGP, bgp->snmp_stats);
966f821c 3801
d62a17ae 3802 XFREE(MTYPE_BGP, bgp);
718e3744 3803}
3804
d62a17ae 3805struct peer *peer_lookup_by_conf_if(struct bgp *bgp, const char *conf_if)
718e3744 3806{
d62a17ae 3807 struct peer *peer;
3808 struct listnode *node, *nnode;
966f821c 3809
d62a17ae 3810 if (!conf_if)
3811 return NULL;
6b0655a2 3812
d62a17ae 3813 if (bgp != NULL) {
3814 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3815 if (peer->conf_if && !strcmp(peer->conf_if, conf_if)
3816 && !CHECK_FLAG(peer->sflags,
3817 PEER_STATUS_ACCEPT_PEER))
3818 return peer;
3819 } else if (bm->bgp != NULL) {
3820 struct listnode *bgpnode, *nbgpnode;
3821
3822 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp))
3823 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3824 if (peer->conf_if
3825 && !strcmp(peer->conf_if, conf_if)
3826 && !CHECK_FLAG(peer->sflags,
3827 PEER_STATUS_ACCEPT_PEER))
3828 return peer;
3829 }
3830 return NULL;
718e3744 3831}
3832
d62a17ae 3833struct peer *peer_lookup_by_hostname(struct bgp *bgp, const char *hostname)
718e3744 3834{
d62a17ae 3835 struct peer *peer;
3836 struct listnode *node, *nnode;
6b0655a2 3837
d62a17ae 3838 if (!hostname)
3839 return NULL;
6b0655a2 3840
d62a17ae 3841 if (bgp != NULL) {
3842 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3843 if (peer->hostname && !strcmp(peer->hostname, hostname)
3844 && !CHECK_FLAG(peer->sflags,
3845 PEER_STATUS_ACCEPT_PEER))
3846 return peer;
3847 } else if (bm->bgp != NULL) {
3848 struct listnode *bgpnode, *nbgpnode;
3849
3850 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp))
3851 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3852 if (peer->hostname
3853 && !strcmp(peer->hostname, hostname)
3854 && !CHECK_FLAG(peer->sflags,
3855 PEER_STATUS_ACCEPT_PEER))
3856 return peer;
3857 }
3858 return NULL;
3859}
3860
3861struct peer *peer_lookup(struct bgp *bgp, union sockunion *su)
3862{
3863 struct peer *peer = NULL;
3864 struct peer tmp_peer;
3865
3866 memset(&tmp_peer, 0, sizeof(struct peer));
3867
3868 /*
3869 * We do not want to find the doppelganger peer so search for the peer
3870 * in
3871 * the hash that has PEER_FLAG_CONFIG_NODE
3872 */
3873 SET_FLAG(tmp_peer.flags, PEER_FLAG_CONFIG_NODE);
3874
3875 tmp_peer.su = *su;
3876
3877 if (bgp != NULL) {
3878 peer = hash_lookup(bgp->peerhash, &tmp_peer);
3879 } else if (bm->bgp != NULL) {
3880 struct listnode *bgpnode, *nbgpnode;
3881
3882 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp)) {
d62a17ae 3883 peer = hash_lookup(bgp->peerhash, &tmp_peer);
d62a17ae 3884 if (peer)
3885 break;
3886 }
3887 }
3888
3889 return peer;
3890}
3891
3892struct peer *peer_create_bind_dynamic_neighbor(struct bgp *bgp,
3893 union sockunion *su,
3894 struct peer_group *group)
3895{
3896 struct peer *peer;
3897 afi_t afi;
3898 safi_t safi;
3899
3900 /* Create peer first; we've already checked group config is valid. */
3901 peer = peer_create(su, NULL, bgp, bgp->as, group->conf->as,
e84c59af 3902 group->conf->as_type, group);
d62a17ae 3903 if (!peer)
3904 return NULL;
3905
3906 /* Link to group */
3907 peer = peer_lock(peer);
3908 listnode_add(group->peer, peer);
3909
3910 peer_group2peer_config_copy(group, peer);
3911
3912 /*
3913 * Bind peer for all AFs configured for the group. We don't call
3914 * peer_group_bind as that is sub-optimal and does some stuff we don't
3915 * want.
3916 */
05c7a1cc
QY
3917 FOREACH_AFI_SAFI (afi, safi) {
3918 if (!group->conf->afc[afi][safi])
3919 continue;
3920 peer->afc[afi][safi] = 1;
d62a17ae 3921
05c7a1cc
QY
3922 if (!peer_af_find(peer, afi, safi))
3923 peer_af_create(peer, afi, safi);
d62a17ae 3924
05c7a1cc
QY
3925 peer_group2peer_config_copy_af(group, peer, afi, safi);
3926 }
d62a17ae 3927
3928 /* Mark as dynamic, but also as a "config node" for other things to
3929 * work. */
3930 SET_FLAG(peer->flags, PEER_FLAG_DYNAMIC_NEIGHBOR);
3931 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
3932
3933 return peer;
3934}
3935
3936struct prefix *
3937peer_group_lookup_dynamic_neighbor_range(struct peer_group *group,
3938 struct prefix *prefix)
3939{
3940 struct listnode *node, *nnode;
3941 struct prefix *range;
3942 afi_t afi;
3943
3944 afi = family2afi(prefix->family);
3945
3946 if (group->listen_range[afi])
3947 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode,
3948 range))
3949 if (prefix_match(range, prefix))
3950 return range;
3951
3952 return NULL;
3953}
3954
3955struct peer_group *
3956peer_group_lookup_dynamic_neighbor(struct bgp *bgp, struct prefix *prefix,
3957 struct prefix **listen_range)
3958{
3959 struct prefix *range = NULL;
3960 struct peer_group *group = NULL;
3961 struct listnode *node, *nnode;
3962
3963 *listen_range = NULL;
3964 if (bgp != NULL) {
3965 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group))
3966 if ((range = peer_group_lookup_dynamic_neighbor_range(
3967 group, prefix)))
3968 break;
3969 } else if (bm->bgp != NULL) {
3970 struct listnode *bgpnode, *nbgpnode;
3971
3972 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp))
3973 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group))
3974 if ((range = peer_group_lookup_dynamic_neighbor_range(
3975 group, prefix)))
3976 goto found_range;
3977 }
3978
3979found_range:
3980 *listen_range = range;
3981 return (group && range) ? group : NULL;
3982}
3983
3984struct peer *peer_lookup_dynamic_neighbor(struct bgp *bgp, union sockunion *su)
3985{
3986 struct peer_group *group;
3987 struct bgp *gbgp;
3988 struct peer *peer;
3989 struct prefix prefix;
3990 struct prefix *listen_range;
3991 int dncount;
3992 char buf[PREFIX2STR_BUFFER];
d62a17ae 3993
0154d8ce
DS
3994 if (!sockunion2hostprefix(su, &prefix))
3995 return NULL;
d62a17ae 3996
3997 /* See if incoming connection matches a configured listen range. */
3998 group = peer_group_lookup_dynamic_neighbor(bgp, &prefix, &listen_range);
3999
4000 if (!group)
4001 return NULL;
4002
4003
4004 gbgp = group->bgp;
4005
4006 if (!gbgp)
4007 return NULL;
4008
4009 prefix2str(&prefix, buf, sizeof(buf));
d62a17ae 4010
4011 if (bgp_debug_neighbor_events(NULL))
4012 zlog_debug(
2dbe669b
DA
4013 "Dynamic Neighbor %s matches group %s listen range %pFX",
4014 buf, group->name, listen_range);
d62a17ae 4015
4016 /* Are we within the listen limit? */
4017 dncount = gbgp->dynamic_neighbors_count;
4018
4019 if (dncount >= gbgp->dynamic_neighbors_limit) {
4020 if (bgp_debug_neighbor_events(NULL))
4021 zlog_debug("Dynamic Neighbor %s rejected - at limit %d",
4022 inet_sutop(su, buf),
4023 gbgp->dynamic_neighbors_limit);
4024 return NULL;
4025 }
4026
4027 /* Ensure group is not disabled. */
4028 if (CHECK_FLAG(group->conf->flags, PEER_FLAG_SHUTDOWN)) {
4029 if (bgp_debug_neighbor_events(NULL))
4030 zlog_debug(
4031 "Dynamic Neighbor %s rejected - group %s disabled",
4032 buf, group->name);
4033 return NULL;
4034 }
4035
4036 /* Check that at least one AF is activated for the group. */
4037 if (!peer_group_af_configured(group)) {
4038 if (bgp_debug_neighbor_events(NULL))
4039 zlog_debug(
4040 "Dynamic Neighbor %s rejected - no AF activated for group %s",
4041 buf, group->name);
4042 return NULL;
4043 }
4044
4045 /* Create dynamic peer and bind to associated group. */
4046 peer = peer_create_bind_dynamic_neighbor(gbgp, su, group);
4047 assert(peer);
4048
4049 gbgp->dynamic_neighbors_count = ++dncount;
4050
4051 if (bgp_debug_neighbor_events(peer))
4052 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4053 peer->host, group->name, dncount);
4054
4055 return peer;
4056}
4057
cc32742c 4058static void peer_drop_dynamic_neighbor(struct peer *peer)
d62a17ae 4059{
4060 int dncount = -1;
cc32742c 4061 if (peer->group->bgp) {
d62a17ae 4062 dncount = peer->group->bgp->dynamic_neighbors_count;
4063 if (dncount)
4064 peer->group->bgp->dynamic_neighbors_count = --dncount;
4065 }
4066 if (bgp_debug_neighbor_events(peer))
4067 zlog_debug("%s dropped from group %s, count %d", peer->host,
4068 peer->group->name, dncount);
4069}
4070
d62a17ae 4071/* If peer is configured at least one address family return 1. */
3dc339cd 4072bool peer_active(struct peer *peer)
d62a17ae 4073{
4074 if (BGP_PEER_SU_UNSPEC(peer))
3dc339cd 4075 return false;
d62a17ae 4076 if (peer->afc[AFI_IP][SAFI_UNICAST] || peer->afc[AFI_IP][SAFI_MULTICAST]
4077 || peer->afc[AFI_IP][SAFI_LABELED_UNICAST]
4078 || peer->afc[AFI_IP][SAFI_MPLS_VPN] || peer->afc[AFI_IP][SAFI_ENCAP]
7c40bf39 4079 || peer->afc[AFI_IP][SAFI_FLOWSPEC]
d62a17ae 4080 || peer->afc[AFI_IP6][SAFI_UNICAST]
4081 || peer->afc[AFI_IP6][SAFI_MULTICAST]
4082 || peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]
4083 || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
4084 || peer->afc[AFI_IP6][SAFI_ENCAP]
7c40bf39 4085 || peer->afc[AFI_IP6][SAFI_FLOWSPEC]
d62a17ae 4086 || peer->afc[AFI_L2VPN][SAFI_EVPN])
3dc339cd
DA
4087 return true;
4088 return false;
d62a17ae 4089}
4090
4091/* If peer is negotiated at least one address family return 1. */
3dc339cd 4092bool peer_active_nego(struct peer *peer)
d62a17ae 4093{
4094 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
4095 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
4096 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
4097 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
4098 || peer->afc_nego[AFI_IP][SAFI_ENCAP]
7c40bf39 4099 || peer->afc_nego[AFI_IP][SAFI_FLOWSPEC]
d62a17ae 4100 || peer->afc_nego[AFI_IP6][SAFI_UNICAST]
4101 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
4102 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
4103 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
4104 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]
7c40bf39 4105 || peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC]
d62a17ae 4106 || peer->afc_nego[AFI_L2VPN][SAFI_EVPN])
3dc339cd
DA
4107 return true;
4108 return false;
d62a17ae 4109}
4110
10711563
DA
4111/* If peer received at least one address family MP, return true */
4112bool peer_afc_received(struct peer *peer)
4113{
4114 afi_t afi;
4115 safi_t safi;
4116
4117 FOREACH_AFI_SAFI (afi, safi)
4118 if (peer->afc_recv[afi][safi])
4119 return true;
4120
4121 return false;
4122}
4123
4124/* If peer advertised at least one address family MP, return true */
4125bool peer_afc_advertised(struct peer *peer)
4126{
4127 afi_t afi;
4128 safi_t safi;
4129
4130 FOREACH_AFI_SAFI (afi, safi)
4131 if (peer->afc_adv[afi][safi])
4132 return true;
4133
4134 return false;
4135}
4136
dcc68b5e 4137void peer_change_action(struct peer *peer, afi_t afi, safi_t safi,
d62a17ae 4138 enum peer_change_type type)
4139{
8b56739e
DA
4140 struct peer_af *paf;
4141
d62a17ae 4142 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
4143 return;
4144
feb17238 4145 if (!peer_established(peer))
d62a17ae 4146 return;
4147
4148 if (type == peer_change_reset) {
4149 /* If we're resetting session, we've to delete both peer struct
4150 */
4151 if ((peer->doppelganger)
4152 && (peer->doppelganger->status != Deleted)
4153 && (!CHECK_FLAG(peer->doppelganger->flags,
4154 PEER_FLAG_CONFIG_NODE)))
4155 peer_delete(peer->doppelganger);
4156
4157 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4158 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4159 } else if (type == peer_change_reset_in) {
4160 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
4161 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
9af52ccf
DA
4162 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
4163 BGP_ROUTE_REFRESH_NORMAL);
d62a17ae 4164 else {
4165 if ((peer->doppelganger)
4166 && (peer->doppelganger->status != Deleted)
4167 && (!CHECK_FLAG(peer->doppelganger->flags,
4168 PEER_FLAG_CONFIG_NODE)))
4169 peer_delete(peer->doppelganger);
4170
4171 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4172 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4173 }
4174 } else if (type == peer_change_reset_out) {
8b56739e
DA
4175 paf = peer_af_find(peer, afi, safi);
4176 if (paf && paf->subgroup)
4177 SET_FLAG(paf->subgroup->sflags,
4178 SUBGRP_STATUS_FORCE_UPDATES);
4179
4180 update_group_adjust_peer(paf);
e1a32ec1 4181 bgp_announce_route(peer, afi, safi, false);
d62a17ae 4182 }
4183}
4184
4185struct peer_flag_action {
4186 /* Peer's flag. */
d7c0a89a 4187 uint32_t flag;
d62a17ae 4188
4189 /* This flag can be set for peer-group member. */
d7c0a89a 4190 uint8_t not_for_member;
d62a17ae 4191
4192 /* Action when the flag is changed. */
4193 enum peer_change_type type;
d62a17ae 4194};
4195
4196static const struct peer_flag_action peer_flag_action_list[] = {
4197 {PEER_FLAG_PASSIVE, 0, peer_change_reset},
4198 {PEER_FLAG_SHUTDOWN, 0, peer_change_reset},
8336c896 4199 {PEER_FLAG_RTT_SHUTDOWN, 0, peer_change_none},
d62a17ae 4200 {PEER_FLAG_DONT_CAPABILITY, 0, peer_change_none},
4201 {PEER_FLAG_OVERRIDE_CAPABILITY, 0, peer_change_none},
4202 {PEER_FLAG_STRICT_CAP_MATCH, 0, peer_change_none},
4203 {PEER_FLAG_DYNAMIC_CAPABILITY, 0, peer_change_reset},
4204 {PEER_FLAG_DISABLE_CONNECTED_CHECK, 0, peer_change_reset},
4205 {PEER_FLAG_CAPABILITY_ENHE, 0, peer_change_reset},
47cbc09b 4206 {PEER_FLAG_ENFORCE_FIRST_AS, 0, peer_change_reset_in},
afad5ced 4207 {PEER_FLAG_IFPEER_V6ONLY, 0, peer_change_reset},
b90a8e13
PM
4208 {PEER_FLAG_ROUTEADV, 0, peer_change_none},
4209 {PEER_FLAG_TIMER, 0, peer_change_none},
4210 {PEER_FLAG_TIMER_CONNECT, 0, peer_change_none},
6c537a18 4211 {PEER_FLAG_TIMER_DELAYOPEN, 0, peer_change_none},
a14810f4
PM
4212 {PEER_FLAG_PASSWORD, 0, peer_change_none},
4213 {PEER_FLAG_LOCAL_AS, 0, peer_change_none},
4214 {PEER_FLAG_LOCAL_AS_NO_PREPEND, 0, peer_change_none},
4215 {PEER_FLAG_LOCAL_AS_REPLACE_AS, 0, peer_change_none},
4216 {PEER_FLAG_UPDATE_SOURCE, 0, peer_change_none},
27aa23a4 4217 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE, 0, peer_change_none},
d08c0c80 4218 {PEER_FLAG_EXTENDED_OPT_PARAMS, 0, peer_change_reset},
d62a17ae 4219 {0, 0, 0}};
4220
4221static const struct peer_flag_action peer_af_flag_action_list[] = {
4222 {PEER_FLAG_SEND_COMMUNITY, 1, peer_change_reset_out},
4223 {PEER_FLAG_SEND_EXT_COMMUNITY, 1, peer_change_reset_out},
4224 {PEER_FLAG_SEND_LARGE_COMMUNITY, 1, peer_change_reset_out},
4225 {PEER_FLAG_NEXTHOP_SELF, 1, peer_change_reset_out},
4226 {PEER_FLAG_REFLECTOR_CLIENT, 1, peer_change_reset},
4227 {PEER_FLAG_RSERVER_CLIENT, 1, peer_change_reset},
4228 {PEER_FLAG_SOFT_RECONFIG, 0, peer_change_reset_in},
4229 {PEER_FLAG_AS_PATH_UNCHANGED, 1, peer_change_reset_out},
4230 {PEER_FLAG_NEXTHOP_UNCHANGED, 1, peer_change_reset_out},
4231 {PEER_FLAG_MED_UNCHANGED, 1, peer_change_reset_out},
598ce6bd 4232 {PEER_FLAG_DEFAULT_ORIGINATE, 0, peer_change_none},
d62a17ae 4233 {PEER_FLAG_REMOVE_PRIVATE_AS, 1, peer_change_reset_out},
4234 {PEER_FLAG_ALLOWAS_IN, 0, peer_change_reset_in},
4235 {PEER_FLAG_ALLOWAS_IN_ORIGIN, 0, peer_change_reset_in},
4236 {PEER_FLAG_ORF_PREFIX_SM, 1, peer_change_reset},
4237 {PEER_FLAG_ORF_PREFIX_RM, 1, peer_change_reset},
598ce6bd
PM
4238 {PEER_FLAG_MAX_PREFIX, 0, peer_change_none},
4239 {PEER_FLAG_MAX_PREFIX_WARNING, 0, peer_change_none},
9cbd06e0 4240 {PEER_FLAG_MAX_PREFIX_FORCE, 0, peer_change_none},
80444d30 4241 {PEER_FLAG_MAX_PREFIX_OUT, 0, peer_change_none},
d62a17ae 4242 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED, 0, peer_change_reset_out},
4243 {PEER_FLAG_FORCE_NEXTHOP_SELF, 1, peer_change_reset_out},
4244 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL, 1, peer_change_reset_out},
4245 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE, 1, peer_change_reset_out},
4246 {PEER_FLAG_AS_OVERRIDE, 1, peer_change_reset_out},
4247 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE, 1, peer_change_reset_out},
d62a17ae 4248 {PEER_FLAG_WEIGHT, 0, peer_change_reset_in},
7c0e4312 4249 {PEER_FLAG_DISABLE_ADDPATH_RX, 0, peer_change_reset},
d62a17ae 4250 {0, 0, 0}};
4251
4252/* Proper action set. */
4253static int peer_flag_action_set(const struct peer_flag_action *action_list,
4254 int size, struct peer_flag_action *action,
d7c0a89a 4255 uint32_t flag)
d62a17ae 4256{
4257 int i;
4258 int found = 0;
4259 int reset_in = 0;
4260 int reset_out = 0;
4261 const struct peer_flag_action *match = NULL;
4262
4263 /* Check peer's frag action. */
4264 for (i = 0; i < size; i++) {
4265 match = &action_list[i];
4266
4267 if (match->flag == 0)
4268 break;
4269
4270 if (match->flag & flag) {
4271 found = 1;
4272
4273 if (match->type == peer_change_reset_in)
4274 reset_in = 1;
4275 if (match->type == peer_change_reset_out)
4276 reset_out = 1;
4277 if (match->type == peer_change_reset) {
4278 reset_in = 1;
4279 reset_out = 1;
4280 }
4281 if (match->not_for_member)
4282 action->not_for_member = 1;
4283 }
4284 }
4285
4286 /* Set peer clear type. */
4287 if (reset_in && reset_out)
4288 action->type = peer_change_reset;
4289 else if (reset_in)
4290 action->type = peer_change_reset_in;
4291 else if (reset_out)
4292 action->type = peer_change_reset_out;
4293 else
4294 action->type = peer_change_none;
4295
4296 return found;
4297}
4298
d7c0a89a 4299static void peer_flag_modify_action(struct peer *peer, uint32_t flag)
d62a17ae 4300{
4301 if (flag == PEER_FLAG_SHUTDOWN) {
4302 if (CHECK_FLAG(peer->flags, flag)) {
4303 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT))
4304 peer_nsf_stop(peer);
4305
4306 UNSET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
8336c896 4307
d62a17ae 4308 if (peer->t_pmax_restart) {
4309 BGP_TIMER_OFF(peer->t_pmax_restart);
4310 if (bgp_debug_neighbor_events(peer))
4311 zlog_debug(
f70c91dc
DA
4312 "%pBP Maximum-prefix restart timer canceled",
4313 peer);
d62a17ae 4314 }
4315
d62a17ae 4316 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4317 char *msg = peer->tx_shutdown_message;
4318 size_t msglen;
9f33eea3 4319 uint8_t msgbuf[BGP_ADMIN_SHUTDOWN_MSG_LEN + 1];
d62a17ae 4320
4321 if (!msg && peer_group_active(peer))
4322 msg = peer->group->conf
4323 ->tx_shutdown_message;
4324 msglen = msg ? strlen(msg) : 0;
9f33eea3
DA
4325 if (msglen > BGP_ADMIN_SHUTDOWN_MSG_LEN)
4326 msglen = BGP_ADMIN_SHUTDOWN_MSG_LEN;
d62a17ae 4327
4328 if (msglen) {
d62a17ae 4329 msgbuf[0] = msglen;
4330 memcpy(msgbuf + 1, msg, msglen);
4331
4332 bgp_notify_send_with_data(
4333 peer, BGP_NOTIFY_CEASE,
4334 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN,
4335 msgbuf, msglen + 1);
4336 } else
4337 bgp_notify_send(
4338 peer, BGP_NOTIFY_CEASE,
4339 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
4340 } else
4341 bgp_session_reset(peer);
4342 } else {
4343 peer->v_start = BGP_INIT_START_TIMER;
4344 BGP_EVENT_ADD(peer, BGP_Stop);
4345 }
4346 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4347 if (flag == PEER_FLAG_DYNAMIC_CAPABILITY)
4348 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4349 else if (flag == PEER_FLAG_PASSIVE)
4350 peer->last_reset = PEER_DOWN_PASSIVE_CHANGE;
4351 else if (flag == PEER_FLAG_DISABLE_CONNECTED_CHECK)
4352 peer->last_reset = PEER_DOWN_MULTIHOP_CHANGE;
4353
4354 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4355 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4356 } else
4357 bgp_session_reset(peer);
4358}
4359
9cf59432 4360/* Enable global administrative shutdown of all peers of BGP instance */
736b68f3 4361void bgp_shutdown_enable(struct bgp *bgp, const char *msg)
9cf59432
DS
4362{
4363 struct peer *peer;
4364 struct listnode *node;
202a1711
DA
4365 /* length(1) + message(N) */
4366 uint8_t data[BGP_ADMIN_SHUTDOWN_MSG_LEN + 1];
9cf59432
DS
4367
4368 /* do nothing if already shut down */
4369 if (CHECK_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN))
4370 return;
4371
8389c83a
DS
4372 /* informational log message */
4373 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4374 bgp->as);
e03f1f18 4375
9cf59432
DS
4376 /* iterate through peers of BGP instance */
4377 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
4378 /* continue, if peer is already in administrative shutdown. */
4379 if (CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN))
4380 continue;
4381
4382 /* send a RFC 4486 notification message if necessary */
4383 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
ed284e23
DA
4384 if (msg) {
4385 size_t datalen = strlen(msg);
4386
4387 if (datalen > BGP_ADMIN_SHUTDOWN_MSG_LEN)
4388 datalen = BGP_ADMIN_SHUTDOWN_MSG_LEN;
4389
4390 data[0] = datalen;
4391 memcpy(data + 1, msg, datalen);
4392
736b68f3
DS
4393 bgp_notify_send_with_data(
4394 peer, BGP_NOTIFY_CEASE,
202a1711
DA
4395 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN, data,
4396 datalen + 1);
ed284e23 4397 } else {
736b68f3
DS
4398 bgp_notify_send(
4399 peer, BGP_NOTIFY_CEASE,
4400 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
ed284e23 4401 }
9cf59432
DS
4402 }
4403
4404 /* reset start timer to initial value */
4405 peer->v_start = BGP_INIT_START_TIMER;
4406
4407 /* trigger a RFC 4271 ManualStop event */
4408 BGP_EVENT_ADD(peer, BGP_Stop);
4409 }
4410
4411 /* set the BGP instances shutdown flag */
4412 SET_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN);
4413}
4414
4415/* Disable global administrative shutdown of all peers of BGP instance */
4416void bgp_shutdown_disable(struct bgp *bgp)
4417{
9cf59432
DS
4418 /* do nothing if not shut down. */
4419 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN))
4420 return;
4421
8389c83a
DS
4422 /* informational log message */
4423 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4424 bgp->as);
9cf59432
DS
4425
4426 /* clear the BGP instances shutdown flag */
4427 UNSET_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN);
4428}
4429
d62a17ae 4430/* Change specified peer flag. */
d7c0a89a 4431static int peer_flag_modify(struct peer *peer, uint32_t flag, int set)
d62a17ae 4432{
4433 int found;
4434 int size;
9fb964de
PM
4435 bool invert, member_invert;
4436 struct peer *member;
d62a17ae 4437 struct listnode *node, *nnode;
4438 struct peer_flag_action action;
4439
4440 memset(&action, 0, sizeof(struct peer_flag_action));
0d6f7fd6 4441 size = sizeof(peer_flag_action_list) / sizeof(struct peer_flag_action);
d62a17ae 4442
9fb964de 4443 invert = CHECK_FLAG(peer->flags_invert, flag);
d62a17ae 4444 found = peer_flag_action_set(peer_flag_action_list, size, &action,
4445 flag);
4446
9fb964de 4447 /* Abort if no flag action exists. */
d62a17ae 4448 if (!found)
4449 return BGP_ERR_INVALID_FLAG;
4450
9fb964de 4451 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
d62a17ae 4452 if (set && CHECK_FLAG(peer->flags | flag, PEER_FLAG_STRICT_CAP_MATCH)
4453 && CHECK_FLAG(peer->flags | flag, PEER_FLAG_OVERRIDE_CAPABILITY))
4454 return BGP_ERR_PEER_FLAG_CONFLICT;
4455
9fb964de 4456 /* Handle flag updates where desired state matches current state. */
d62a17ae 4457 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
9fb964de
PM
4458 if (set && CHECK_FLAG(peer->flags, flag)) {
4459 COND_FLAG(peer->flags_override, flag, !invert);
d62a17ae 4460 return 0;
9fb964de
PM
4461 }
4462
4463 if (!set && !CHECK_FLAG(peer->flags, flag)) {
4464 COND_FLAG(peer->flags_override, flag, invert);
d62a17ae 4465 return 0;
9fb964de 4466 }
d62a17ae 4467 }
4468
9fb964de
PM
4469 /* Inherit from peer-group or set/unset flags accordingly. */
4470 if (peer_group_active(peer) && set == invert)
4471 peer_flag_inherit(peer, flag);
d62a17ae 4472 else
9fb964de 4473 COND_FLAG(peer->flags, flag, set);
d62a17ae 4474
9fb964de 4475 /* Check if handling a regular peer. */
d62a17ae 4476 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
9fb964de
PM
4477 /* Update flag override state accordingly. */
4478 COND_FLAG(peer->flags_override, flag, set != invert);
4479
b3a3290e
DS
4480 /*
4481 * For the extended next-hop encoding flag we need to turn RAs
4482 * on if flag is being set, but only turn RAs off if the flag
4483 * is being unset on this peer and if this peer is a member of a
4484 * peer-group, the peer-group also doesn't have the flag set.
4485 */
4486 if (flag == PEER_FLAG_CAPABILITY_ENHE) {
4487 if (set) {
4488 bgp_zebra_initiate_radv(peer->bgp, peer);
4489 } else if (peer_group_active(peer)) {
28168b1b
DS
4490 if (!CHECK_FLAG(peer->group->conf->flags,
4491 flag) &&
4492 !peer->conf_if)
b3a3290e
DS
4493 bgp_zebra_terminate_radv(peer->bgp,
4494 peer);
4495 } else
4496 bgp_zebra_terminate_radv(peer->bgp, peer);
4497 }
087a1c6c 4498
9fb964de 4499 /* Execute flag action on peer. */
d62a17ae 4500 if (action.type == peer_change_reset)
4501 peer_flag_modify_action(peer, flag);
4502
9fb964de 4503 /* Skip peer-group mechanics for regular peers. */
d62a17ae 4504 return 0;
4505 }
4506
9fb964de 4507 /*
f75b9163 4508 * Update peer-group members, unless they are explicitly overriding
9fb964de
PM
4509 * peer-group configuration.
4510 */
4511 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
4512 /* Skip peers with overridden configuration. */
4513 if (CHECK_FLAG(member->flags_override, flag))
4514 continue;
d62a17ae 4515
527de3dc
PM
4516 /* Check if only member without group is inverted. */
4517 member_invert =
4518 CHECK_FLAG(member->flags_invert, flag) && !invert;
d62a17ae 4519
9fb964de
PM
4520 /* Skip peers with equivalent configuration. */
4521 if (set != member_invert && CHECK_FLAG(member->flags, flag))
d62a17ae 4522 continue;
4523
9fb964de 4524 if (set == member_invert && !CHECK_FLAG(member->flags, flag))
d62a17ae 4525 continue;
4526
9fb964de
PM
4527 /* Update flag on peer-group member. */
4528 COND_FLAG(member->flags, flag, set != member_invert);
d62a17ae 4529
28168b1b 4530 if (flag == PEER_FLAG_CAPABILITY_ENHE && !member->conf_if)
b3a3290e
DS
4531 set ? bgp_zebra_initiate_radv(member->bgp, member)
4532 : bgp_zebra_terminate_radv(member->bgp, member);
1ea03b90 4533
9fb964de 4534 /* Execute flag action on peer-group member. */
d62a17ae 4535 if (action.type == peer_change_reset)
9fb964de 4536 peer_flag_modify_action(member, flag);
d62a17ae 4537 }
9fb964de 4538
d62a17ae 4539 return 0;
4540}
4541
d7c0a89a 4542int peer_flag_set(struct peer *peer, uint32_t flag)
d62a17ae 4543{
4544 return peer_flag_modify(peer, flag, 1);
4545}
4546
d7c0a89a 4547int peer_flag_unset(struct peer *peer, uint32_t flag)
d62a17ae 4548{
4549 return peer_flag_modify(peer, flag, 0);
4550}
4551
4552static int peer_af_flag_modify(struct peer *peer, afi_t afi, safi_t safi,
27c05d4d 4553 uint32_t flag, bool set)
d62a17ae 4554{
4555 int found;
4556 int size;
527de3dc 4557 bool invert, member_invert;
527de3dc 4558 struct peer *member;
d62a17ae 4559 struct listnode *node, *nnode;
d62a17ae 4560 struct peer_flag_action action;
3061acc2 4561 enum bgp_peer_sort ptype;
d62a17ae 4562
4563 memset(&action, 0, sizeof(struct peer_flag_action));
0d6f7fd6 4564 size = sizeof(peer_af_flag_action_list)
d62a17ae 4565 / sizeof(struct peer_flag_action);
4566
27c05d4d 4567 invert = CHECK_FLAG(peer->af_flags_invert[afi][safi], flag);
d62a17ae 4568 found = peer_flag_action_set(peer_af_flag_action_list, size, &action,
4569 flag);
4570
527de3dc 4571 /* Abort if flag action exists. */
d62a17ae 4572 if (!found)
4573 return BGP_ERR_INVALID_FLAG;
4574
469cbb0f 4575 ptype = peer_sort(peer);
d62a17ae 4576 /* Special check for reflector client. */
469cbb0f 4577 if (flag & PEER_FLAG_REFLECTOR_CLIENT && ptype != BGP_PEER_IBGP)
d62a17ae 4578 return BGP_ERR_NOT_INTERNAL_PEER;
4579
4580 /* Special check for remove-private-AS. */
469cbb0f 4581 if (flag & PEER_FLAG_REMOVE_PRIVATE_AS && ptype == BGP_PEER_IBGP)
d62a17ae 4582 return BGP_ERR_REMOVE_PRIVATE_AS;
4583
4584 /* as-override is not allowed for IBGP peers */
469cbb0f 4585 if (flag & PEER_FLAG_AS_OVERRIDE && ptype == BGP_PEER_IBGP)
d62a17ae 4586 return BGP_ERR_AS_OVERRIDE;
4587
527de3dc 4588 /* Handle flag updates where desired state matches current state. */
d62a17ae 4589 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
598ce6bd 4590 if (set && CHECK_FLAG(peer->af_flags[afi][safi], flag)) {
527de3dc
PM
4591 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4592 !invert);
d62a17ae 4593 return 0;
598ce6bd
PM
4594 }
4595
4596 if (!set && !CHECK_FLAG(peer->af_flags[afi][safi], flag)) {
527de3dc
PM
4597 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4598 invert);
d62a17ae 4599 return 0;
598ce6bd 4600 }
d62a17ae 4601 }
4602
ace295a9
MK
4603 /*
4604 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4605 * if we are setting/unsetting flags which conflict with this flag
4606 * handle accordingly
4607 */
4608 if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
4609 if (set) {
4610
3518f352
DS
4611 /*
4612 * if we are setting NEXTHOP_SELF, we need to unset the
4613 * NEXTHOP_UNCHANGED flag
4614 */
ace295a9
MK
4615 if (CHECK_FLAG(flag, PEER_FLAG_NEXTHOP_SELF) ||
4616 CHECK_FLAG(flag, PEER_FLAG_FORCE_NEXTHOP_SELF))
4617 UNSET_FLAG(peer->af_flags[afi][safi],
4618 PEER_FLAG_NEXTHOP_UNCHANGED);
4619 } else {
4620
3518f352
DS
4621 /*
4622 * if we are unsetting NEXTHOP_SELF, we need to set the
ace295a9
MK
4623 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4624 */
4625 if (CHECK_FLAG(flag, PEER_FLAG_NEXTHOP_SELF) ||
4626 CHECK_FLAG(flag, PEER_FLAG_FORCE_NEXTHOP_SELF))
4627 SET_FLAG(peer->af_flags[afi][safi],
4628 PEER_FLAG_NEXTHOP_UNCHANGED);
4629 }
4630 }
4631
99819027
DS
4632 /*
4633 * If the peer is a route server client let's not
4634 * muck with the nexthop on the way out the door
4635 */
4636 if (flag & PEER_FLAG_RSERVER_CLIENT) {
4637 if (set)
4638 SET_FLAG(peer->af_flags[afi][safi],
4639 PEER_FLAG_NEXTHOP_UNCHANGED);
4640 else
4641 UNSET_FLAG(peer->af_flags[afi][safi],
4642 PEER_FLAG_NEXTHOP_UNCHANGED);
4643 }
4644
527de3dc
PM
4645 /* Inherit from peer-group or set/unset flags accordingly. */
4646 if (peer_group_active(peer) && set == invert)
4647 peer_af_flag_inherit(peer, afi, safi, flag);
4648 else
4649 COND_FLAG(peer->af_flags[afi][safi], flag, set);
d62a17ae 4650
4651 /* Execute action when peer is established. */
4652 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
feb17238 4653 && peer_established(peer)) {
d62a17ae 4654 if (!set && flag == PEER_FLAG_SOFT_RECONFIG)
4655 bgp_clear_adj_in(peer, afi, safi);
4656 else {
4657 if (flag == PEER_FLAG_REFLECTOR_CLIENT)
4658 peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE;
4659 else if (flag == PEER_FLAG_RSERVER_CLIENT)
4660 peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE;
4661 else if (flag == PEER_FLAG_ORF_PREFIX_SM)
4662 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4663 else if (flag == PEER_FLAG_ORF_PREFIX_RM)
4664 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4665
4666 peer_change_action(peer, afi, safi, action.type);
4667 }
4668 }
4669
527de3dc
PM
4670 /* Check if handling a regular peer. */
4671 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4672 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4673 set != invert);
4674 } else {
4675 /*
f75b9163 4676 * Update peer-group members, unless they are explicitly
527de3dc
PM
4677 * overriding peer-group configuration.
4678 */
4679 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode,
4680 member)) {
4681 /* Skip peers with overridden configuration. */
4682 if (CHECK_FLAG(member->af_flags_override[afi][safi],
598ce6bd
PM
4683 flag))
4684 continue;
4685
527de3dc
PM
4686 /* Check if only member without group is inverted. */
4687 member_invert =
4688 CHECK_FLAG(member->af_flags_invert[afi][safi],
4689 flag)
4690 && !invert;
4691
4692 /* Skip peers with equivalent configuration. */
4693 if (set != member_invert
4694 && CHECK_FLAG(member->af_flags[afi][safi], flag))
d62a17ae 4695 continue;
4696
527de3dc
PM
4697 if (set == member_invert
4698 && !CHECK_FLAG(member->af_flags[afi][safi], flag))
d62a17ae 4699 continue;
4700
527de3dc
PM
4701 /* Update flag on peer-group member. */
4702 COND_FLAG(member->af_flags[afi][safi], flag,
4703 set != member_invert);
d62a17ae 4704
527de3dc 4705 /* Execute flag action on peer-group member. */
feb17238 4706 if (peer_established(member)) {
d62a17ae 4707 if (!set && flag == PEER_FLAG_SOFT_RECONFIG)
527de3dc 4708 bgp_clear_adj_in(member, afi, safi);
d62a17ae 4709 else {
4710 if (flag == PEER_FLAG_REFLECTOR_CLIENT)
527de3dc 4711 member->last_reset =
d62a17ae 4712 PEER_DOWN_RR_CLIENT_CHANGE;
4713 else if (flag
4714 == PEER_FLAG_RSERVER_CLIENT)
527de3dc 4715 member->last_reset =
d62a17ae 4716 PEER_DOWN_RS_CLIENT_CHANGE;
4717 else if (flag
4718 == PEER_FLAG_ORF_PREFIX_SM)
527de3dc 4719 member->last_reset =
d62a17ae 4720 PEER_DOWN_CAPABILITY_CHANGE;
4721 else if (flag
4722 == PEER_FLAG_ORF_PREFIX_RM)
527de3dc 4723 member->last_reset =
d62a17ae 4724 PEER_DOWN_CAPABILITY_CHANGE;
4725
527de3dc 4726 peer_change_action(member, afi, safi,
d62a17ae 4727 action.type);
4728 }
4729 }
4730 }
4731 }
4732
d62a17ae 4733 return 0;
4734}
4735
d7c0a89a 4736int peer_af_flag_set(struct peer *peer, afi_t afi, safi_t safi, uint32_t flag)
d62a17ae 4737{
4738 return peer_af_flag_modify(peer, afi, safi, flag, 1);
4739}
4740
d7c0a89a 4741int peer_af_flag_unset(struct peer *peer, afi_t afi, safi_t safi, uint32_t flag)
d62a17ae 4742{
4743 return peer_af_flag_modify(peer, afi, safi, flag, 0);
4744}
4745
4746
3dc339cd 4747void peer_tx_shutdown_message_set(struct peer *peer, const char *msg)
d62a17ae 4748{
4749 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
4750 peer->tx_shutdown_message =
4751 msg ? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG, msg) : NULL;
d62a17ae 4752}
4753
3dc339cd 4754void peer_tx_shutdown_message_unset(struct peer *peer)
d62a17ae 4755{
4756 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
d62a17ae 4757}
4758
4759
4760/* EBGP multihop configuration. */
4761int peer_ebgp_multihop_set(struct peer *peer, int ttl)
4762{
4763 struct peer_group *group;
4764 struct listnode *node, *nnode;
4765 struct peer *peer1;
4766
4767 if (peer->sort == BGP_PEER_IBGP || peer->conf_if)
4768 return 0;
8ffeaddb
EDP
4769
4770 /* is there anything to do? */
4771 if (peer->ttl == ttl)
4772 return 0;
d62a17ae 4773
4774 /* see comment in peer_ttl_security_hops_set() */
4775 if (ttl != MAXTTL) {
4776 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4777 group = peer->group;
e2521429 4778 if (group->conf->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
d62a17ae 4779 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4780
4781 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
4782 peer1)) {
4783 if (peer1->sort == BGP_PEER_IBGP)
4784 continue;
4785
e2521429 4786 if (peer1->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
d62a17ae 4787 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4788 }
4789 } else {
e2521429 4790 if (peer->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
d62a17ae 4791 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4792 }
4793 }
4794
4795 peer->ttl = ttl;
4796
4797 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
3557ed3d 4798 if (peer->sort != BGP_PEER_IBGP) {
d62a17ae 4799 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4800 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4801 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4802 else
4803 bgp_session_reset(peer);
4ba37eb6
RZ
4804
4805 /* Reconfigure BFD peer with new TTL. */
4806 if (peer->bfd_config)
4807 bgp_peer_bfd_update_source(peer);
d62a17ae 4808 }
4809 } else {
4810 group = peer->group;
4811 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
4812 if (peer->sort == BGP_PEER_IBGP)
4813 continue;
4814
4815 peer->ttl = group->conf->ttl;
4816
4817 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4818 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4819 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4820 else
4821 bgp_session_reset(peer);
4ba37eb6
RZ
4822
4823 /* Reconfigure BFD peer with new TTL. */
4824 if (peer->bfd_config)
4825 bgp_peer_bfd_update_source(peer);
d62a17ae 4826 }
4827 }
4828 return 0;
4829}
4830
4831int peer_ebgp_multihop_unset(struct peer *peer)
4832{
4833 struct peer_group *group;
4834 struct listnode *node, *nnode;
6e26b2e2 4835 int ttl;
d62a17ae 4836
4837 if (peer->sort == BGP_PEER_IBGP)
4838 return 0;
4839
e2521429 4840 if (peer->gtsm_hops != BGP_GTSM_HOPS_DISABLED && peer->ttl != MAXTTL)
d62a17ae 4841 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4842
4843 if (peer_group_active(peer))
6e26b2e2 4844 ttl = peer->group->conf->ttl;
d62a17ae 4845 else
6e26b2e2
DS
4846 ttl = BGP_DEFAULT_TTL;
4847
4848 if (ttl == peer->ttl)
4849 return 0;
4850
4851 peer->ttl = ttl;
d62a17ae 4852
4853 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4854 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4855 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4856 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4857 else
4858 bgp_session_reset(peer);
4ba37eb6
RZ
4859
4860 /* Reconfigure BFD peer with new TTL. */
4861 if (peer->bfd_config)
4862 bgp_peer_bfd_update_source(peer);
d62a17ae 4863 } else {
4864 group = peer->group;
4865 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
4866 if (peer->sort == BGP_PEER_IBGP)
4867 continue;
4868
a0903cd2 4869 peer->ttl = BGP_DEFAULT_TTL;
d62a17ae 4870
4871 if (peer->fd >= 0) {
4872 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4873 bgp_notify_send(
4874 peer, BGP_NOTIFY_CEASE,
4875 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4876 else
4877 bgp_session_reset(peer);
4878 }
4ba37eb6
RZ
4879
4880 /* Reconfigure BFD peer with new TTL. */
4881 if (peer->bfd_config)
4882 bgp_peer_bfd_update_source(peer);
d62a17ae 4883 }
4884 }
4885 return 0;
4886}
4887
4888/* Neighbor description. */
3dc339cd 4889void peer_description_set(struct peer *peer, const char *desc)
d62a17ae 4890{
0a22ddfb 4891 XFREE(MTYPE_PEER_DESC, peer->desc);
d62a17ae 4892
4893 peer->desc = XSTRDUP(MTYPE_PEER_DESC, desc);
d62a17ae 4894}
4895
3dc339cd 4896void peer_description_unset(struct peer *peer)
d62a17ae 4897{
0a22ddfb 4898 XFREE(MTYPE_PEER_DESC, peer->desc);
d62a17ae 4899}
4900
4901/* Neighbor update-source. */
4902int peer_update_source_if_set(struct peer *peer, const char *ifname)
4903{
a14810f4 4904 struct peer *member;
d62a17ae 4905 struct listnode *node, *nnode;
4906
a14810f4
PM
4907 /* Set flag and configuration on peer. */
4908 peer_flag_set(peer, PEER_FLAG_UPDATE_SOURCE);
d62a17ae 4909 if (peer->update_if) {
a14810f4 4910 if (strcmp(peer->update_if, ifname) == 0)
d62a17ae 4911 return 0;
d62a17ae 4912 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
d62a17ae 4913 }
d62a17ae 4914 peer->update_if = XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, ifname);
a14810f4
PM
4915 sockunion_free(peer->update_source);
4916 peer->update_source = NULL;
d62a17ae 4917
a14810f4 4918 /* Check if handling a regular peer. */
d62a17ae 4919 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 4920 /* Send notification or reset peer depending on state. */
d62a17ae 4921 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4922 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
4923 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4924 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4925 } else
4926 bgp_session_reset(peer);
a14810f4 4927
7196f56e
RZ
4928 /* Apply new source configuration to BFD session. */
4929 if (peer->bfd_config)
4930 bgp_peer_bfd_update_source(peer);
4931
a14810f4 4932 /* Skip peer-group mechanics for regular peers. */
d62a17ae 4933 return 0;
4934 }
4935
a14810f4
PM
4936 /*
4937 * Set flag and configuration on all peer-group members, unless they are
f75b9163 4938 * explicitly overriding peer-group configuration.
a14810f4
PM
4939 */
4940 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
4941 /* Skip peers with overridden configuration. */
4942 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
4943 continue;
d62a17ae 4944
a14810f4
PM
4945 /* Skip peers with the same configuration. */
4946 if (member->update_if) {
4947 if (strcmp(member->update_if, ifname) == 0)
4948 continue;
4949 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
d62a17ae 4950 }
4951
a14810f4
PM
4952 /* Set flag and configuration on peer-group member. */
4953 SET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
4954 member->update_if = XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, ifname);
4955 sockunion_free(member->update_source);
4956 member->update_source = NULL;
4957
4958 /* Send notification or reset peer depending on state. */
4959 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
4960 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
4961 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 4962 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4963 } else
a14810f4 4964 bgp_session_reset(member);
7196f56e
RZ
4965
4966 /* Apply new source configuration to BFD session. */
4967 if (member->bfd_config)
4968 bgp_peer_bfd_update_source(member);
d62a17ae 4969 }
a14810f4 4970
d62a17ae 4971 return 0;
4972}
4973
4974int peer_update_source_addr_set(struct peer *peer, const union sockunion *su)
4975{
a14810f4 4976 struct peer *member;
d62a17ae 4977 struct listnode *node, *nnode;
4978
a14810f4
PM
4979 /* Set flag and configuration on peer. */
4980 peer_flag_set(peer, PEER_FLAG_UPDATE_SOURCE);
d62a17ae 4981 if (peer->update_source) {
a14810f4 4982 if (sockunion_cmp(peer->update_source, su) == 0)
d62a17ae 4983 return 0;
4984 sockunion_free(peer->update_source);
d62a17ae 4985 }
d62a17ae 4986 peer->update_source = sockunion_dup(su);
a14810f4 4987 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
d62a17ae 4988
a14810f4 4989 /* Check if handling a regular peer. */
d62a17ae 4990 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 4991 /* Send notification or reset peer depending on state. */
d62a17ae 4992 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4993 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
4994 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4995 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4996 } else
4997 bgp_session_reset(peer);
a14810f4 4998
7196f56e
RZ
4999 /* Apply new source configuration to BFD session. */
5000 if (peer->bfd_config)
5001 bgp_peer_bfd_update_source(peer);
5002
a14810f4 5003 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5004 return 0;
5005 }
5006
a14810f4
PM
5007 /*
5008 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5009 * explicitly overriding peer-group configuration.
a14810f4
PM
5010 */
5011 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5012 /* Skip peers with overridden configuration. */
5013 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5014 continue;
d62a17ae 5015
a14810f4
PM
5016 /* Skip peers with the same configuration. */
5017 if (member->update_source) {
5018 if (sockunion_cmp(member->update_source, su) == 0)
5019 continue;
5020 sockunion_free(member->update_source);
d62a17ae 5021 }
5022
a14810f4
PM
5023 /* Set flag and configuration on peer-group member. */
5024 SET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5025 member->update_source = sockunion_dup(su);
5026 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5027
5028 /* Send notification or reset peer depending on state. */
5029 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5030 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5031 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 5032 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5033 } else
a14810f4 5034 bgp_session_reset(member);
7196f56e
RZ
5035
5036 /* Apply new source configuration to BFD session. */
5037 if (member->bfd_config)
5038 bgp_peer_bfd_update_source(member);
d62a17ae 5039 }
a14810f4 5040
d62a17ae 5041 return 0;
5042}
5043
5044int peer_update_source_unset(struct peer *peer)
5045{
a14810f4 5046 struct peer *member;
d62a17ae 5047 struct listnode *node, *nnode;
5048
a14810f4 5049 if (!CHECK_FLAG(peer->flags, PEER_FLAG_UPDATE_SOURCE))
d62a17ae 5050 return 0;
5051
a14810f4
PM
5052 /* Inherit configuration from peer-group if peer is member. */
5053 if (peer_group_active(peer)) {
5054 peer_flag_inherit(peer, PEER_FLAG_UPDATE_SOURCE);
5055 PEER_SU_ATTR_INHERIT(peer, peer->group, update_source);
5056 PEER_STR_ATTR_INHERIT(peer, peer->group, update_if,
5057 MTYPE_PEER_UPDATE_SOURCE);
5058 } else {
5059 /* Otherwise remove flag and configuration from peer. */
5060 peer_flag_unset(peer, PEER_FLAG_UPDATE_SOURCE);
d62a17ae 5061 sockunion_free(peer->update_source);
5062 peer->update_source = NULL;
d62a17ae 5063 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
d62a17ae 5064 }
5065
a14810f4 5066 /* Check if handling a regular peer. */
d62a17ae 5067 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 5068 /* Send notification or reset peer depending on state. */
d62a17ae 5069 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5070 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5071 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5072 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5073 } else
5074 bgp_session_reset(peer);
a14810f4 5075
7196f56e
RZ
5076 /* Apply new source configuration to BFD session. */
5077 if (peer->bfd_config)
5078 bgp_peer_bfd_update_source(peer);
5079
a14810f4 5080 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5081 return 0;
5082 }
5083
a14810f4
PM
5084 /*
5085 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5086 * explicitly overriding peer-group configuration.
a14810f4
PM
5087 */
5088 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5089 /* Skip peers with overridden configuration. */
5090 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
d62a17ae 5091 continue;
5092
a14810f4
PM
5093 /* Skip peers with the same configuration. */
5094 if (!CHECK_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE)
5095 && !member->update_source && !member->update_if)
5096 continue;
d62a17ae 5097
a14810f4
PM
5098 /* Remove flag and configuration on peer-group member. */
5099 UNSET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5100 sockunion_free(member->update_source);
5101 member->update_source = NULL;
5102 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5103
5104 /* Send notification or reset peer depending on state. */
5105 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5106 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5107 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 5108 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5109 } else
a14810f4 5110 bgp_session_reset(member);
7196f56e
RZ
5111
5112 /* Apply new source configuration to BFD session. */
5113 if (member->bfd_config)
5114 bgp_peer_bfd_update_source(member);
d62a17ae 5115 }
a14810f4 5116
d62a17ae 5117 return 0;
5118}
5119
5120int peer_default_originate_set(struct peer *peer, afi_t afi, safi_t safi,
1de27621 5121 const char *rmap, struct route_map *route_map)
d62a17ae 5122{
598ce6bd 5123 struct peer *member;
d62a17ae 5124 struct listnode *node, *nnode;
5125
598ce6bd
PM
5126 /* Set flag and configuration on peer. */
5127 peer_af_flag_set(peer, afi, safi, PEER_FLAG_DEFAULT_ORIGINATE);
5128 if (rmap) {
5129 if (!peer->default_rmap[afi][safi].name
5130 || strcmp(rmap, peer->default_rmap[afi][safi].name) != 0) {
d62a17ae 5131 if (peer->default_rmap[afi][safi].name)
5132 XFREE(MTYPE_ROUTE_MAP_NAME,
5133 peer->default_rmap[afi][safi].name);
598ce6bd 5134
b4897fa5 5135 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
d62a17ae 5136 peer->default_rmap[afi][safi].name =
5137 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
1de27621 5138 peer->default_rmap[afi][safi].map = route_map;
b4897fa5 5139 route_map_counter_increment(route_map);
d62a17ae 5140 }
598ce6bd
PM
5141 } else if (!rmap) {
5142 if (peer->default_rmap[afi][safi].name)
5143 XFREE(MTYPE_ROUTE_MAP_NAME,
5144 peer->default_rmap[afi][safi].name);
5145
b4897fa5 5146 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
598ce6bd
PM
5147 peer->default_rmap[afi][safi].name = NULL;
5148 peer->default_rmap[afi][safi].map = NULL;
d62a17ae 5149 }
5150
598ce6bd 5151 /* Check if handling a regular peer. */
d62a17ae 5152 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
598ce6bd 5153 /* Update peer route announcements. */
feb17238 5154 if (peer_established(peer) && peer->afc_nego[afi][safi]) {
d62a17ae 5155 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5156 bgp_default_originate(peer, afi, safi, 0);
e1a32ec1 5157 bgp_announce_route(peer, afi, safi, false);
d62a17ae 5158 }
598ce6bd
PM
5159
5160 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5161 return 0;
5162 }
5163
598ce6bd
PM
5164 /*
5165 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5166 * explicitly overriding peer-group configuration.
598ce6bd
PM
5167 */
5168 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5169 /* Skip peers with overridden configuration. */
5170 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5171 PEER_FLAG_DEFAULT_ORIGINATE))
5172 continue;
d62a17ae 5173
598ce6bd
PM
5174 /* Set flag and configuration on peer-group member. */
5175 SET_FLAG(member->af_flags[afi][safi],
5176 PEER_FLAG_DEFAULT_ORIGINATE);
d62a17ae 5177 if (rmap) {
598ce6bd 5178 if (member->default_rmap[afi][safi].name)
d62a17ae 5179 XFREE(MTYPE_ROUTE_MAP_NAME,
598ce6bd 5180 member->default_rmap[afi][safi].name);
b4897fa5 5181 route_map_counter_decrement(
5182 member->default_rmap[afi][safi].map);
598ce6bd 5183 member->default_rmap[afi][safi].name =
d62a17ae 5184 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
1de27621 5185 member->default_rmap[afi][safi].map = route_map;
b4897fa5 5186 route_map_counter_increment(route_map);
d62a17ae 5187 }
5188
598ce6bd 5189 /* Update peer route announcements. */
feb17238 5190 if (peer_established(member) && member->afc_nego[afi][safi]) {
598ce6bd
PM
5191 update_group_adjust_peer(
5192 peer_af_find(member, afi, safi));
5193 bgp_default_originate(member, afi, safi, 0);
e1a32ec1 5194 bgp_announce_route(member, afi, safi, false);
d62a17ae 5195 }
5196 }
598ce6bd 5197
d62a17ae 5198 return 0;
5199}
5200
5201int peer_default_originate_unset(struct peer *peer, afi_t afi, safi_t safi)
5202{
598ce6bd 5203 struct peer *member;
d62a17ae 5204 struct listnode *node, *nnode;
5205
598ce6bd
PM
5206 /* Inherit configuration from peer-group if peer is member. */
5207 if (peer_group_active(peer)) {
5208 peer_af_flag_inherit(peer, afi, safi,
5209 PEER_FLAG_DEFAULT_ORIGINATE);
e7103a96
PM
5210 PEER_STR_ATTR_INHERIT(peer, peer->group,
5211 default_rmap[afi][safi].name,
5212 MTYPE_ROUTE_MAP_NAME);
5213 PEER_ATTR_INHERIT(peer, peer->group,
5214 default_rmap[afi][safi].map);
598ce6bd
PM
5215 } else {
5216 /* Otherwise remove flag and configuration from peer. */
5217 peer_af_flag_unset(peer, afi, safi,
5218 PEER_FLAG_DEFAULT_ORIGINATE);
d62a17ae 5219 if (peer->default_rmap[afi][safi].name)
5220 XFREE(MTYPE_ROUTE_MAP_NAME,
5221 peer->default_rmap[afi][safi].name);
b4897fa5 5222 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
d62a17ae 5223 peer->default_rmap[afi][safi].name = NULL;
5224 peer->default_rmap[afi][safi].map = NULL;
5225 }
5226
598ce6bd 5227 /* Check if handling a regular peer. */
d62a17ae 5228 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
598ce6bd 5229 /* Update peer route announcements. */
feb17238 5230 if (peer_established(peer) && peer->afc_nego[afi][safi]) {
d62a17ae 5231 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5232 bgp_default_originate(peer, afi, safi, 1);
e1a32ec1 5233 bgp_announce_route(peer, afi, safi, false);
d62a17ae 5234 }
598ce6bd
PM
5235
5236 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5237 return 0;
5238 }
5239
598ce6bd
PM
5240 /*
5241 * Remove flag and configuration from all peer-group members, unless
f75b9163 5242 * they are explicitly overriding peer-group configuration.
598ce6bd
PM
5243 */
5244 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5245 /* Skip peers with overridden configuration. */
5246 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5247 PEER_FLAG_DEFAULT_ORIGINATE))
5248 continue;
5249
5250 /* Remove flag and configuration on peer-group member. */
66de9680 5251 UNSET_FLAG(member->af_flags[afi][safi],
d62a17ae 5252 PEER_FLAG_DEFAULT_ORIGINATE);
66de9680 5253 if (member->default_rmap[afi][safi].name)
d62a17ae 5254 XFREE(MTYPE_ROUTE_MAP_NAME,
66de9680
S
5255 member->default_rmap[afi][safi].name);
5256 route_map_counter_decrement(member->default_rmap[afi][safi].map);
5257 member->default_rmap[afi][safi].name = NULL;
5258 member->default_rmap[afi][safi].map = NULL;
d62a17ae 5259
598ce6bd 5260 /* Update peer route announcements. */
feb17238 5261 if (peer_established(member) && member->afc_nego[afi][safi]) {
66de9680
S
5262 update_group_adjust_peer(peer_af_find(member, afi, safi));
5263 bgp_default_originate(member, afi, safi, 1);
e1a32ec1 5264 bgp_announce_route(member, afi, safi, false);
d62a17ae 5265 }
5266 }
598ce6bd 5267
d62a17ae 5268 return 0;
5269}
5270
3dc339cd 5271void peer_port_set(struct peer *peer, uint16_t port)
d62a17ae 5272{
5273 peer->port = port;
d62a17ae 5274}
5275
3dc339cd 5276void peer_port_unset(struct peer *peer)
d62a17ae 5277{
5278 peer->port = BGP_PORT_DEFAULT;
d62a17ae 5279}
5280
4ab46701
AR
5281/* Set the TCP-MSS value in the peer structure,
5282 * This gets applied only after connection reset
5283 * So this value will be used in bgp_connect.
5284 */
5285void peer_tcp_mss_set(struct peer *peer, uint32_t tcp_mss)
5286{
5287 peer->tcp_mss = tcp_mss;
5288 SET_FLAG(peer->flags, PEER_FLAG_TCP_MSS);
5289}
5290
5291/* Reset the TCP-MSS value in the peer structure,
5292 * This gets applied only after connection reset
5293 * So this value will be used in bgp_connect.
5294 */
5295void peer_tcp_mss_unset(struct peer *peer)
5296{
5297 UNSET_FLAG(peer->flags, PEER_FLAG_TCP_MSS);
5298 peer->tcp_mss = 0;
5299}
5300
d62a17ae 5301/*
5302 * Helper function that is called after the name of the policy
5303 * being used by a peer has changed (AF specific). Automatically
5304 * initiates inbound or outbound processing as needed.
5305 */
5306static void peer_on_policy_change(struct peer *peer, afi_t afi, safi_t safi,
5307 int outbound)
5308{
5309 if (outbound) {
5310 update_group_adjust_peer(peer_af_find(peer, afi, safi));
feb17238 5311 if (peer_established(peer))
e1a32ec1 5312 bgp_announce_route(peer, afi, safi, false);
d62a17ae 5313 } else {
feb17238 5314 if (!peer_established(peer))
d62a17ae 5315 return;
5316
5317 if (CHECK_FLAG(peer->af_flags[afi][safi],
5318 PEER_FLAG_SOFT_RECONFIG))
5319 bgp_soft_reconfig_in(peer, afi, safi);
5320 else if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
5321 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
9af52ccf
DA
5322 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
5323 BGP_ROUTE_REFRESH_NORMAL);
d62a17ae 5324 }
5325}
5326
5327
5328/* neighbor weight. */
d7c0a89a 5329int peer_weight_set(struct peer *peer, afi_t afi, safi_t safi, uint16_t weight)
d62a17ae 5330{
598ce6bd 5331 struct peer *member;
d62a17ae 5332 struct listnode *node, *nnode;
5333
598ce6bd
PM
5334 /* Set flag and configuration on peer. */
5335 peer_af_flag_set(peer, afi, safi, PEER_FLAG_WEIGHT);
d62a17ae 5336 if (peer->weight[afi][safi] != weight) {
5337 peer->weight[afi][safi] = weight;
d62a17ae 5338 peer_on_policy_change(peer, afi, safi, 0);
5339 }
5340
598ce6bd 5341 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5342 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5343 return 0;
5344
598ce6bd
PM
5345 /*
5346 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5347 * explicitly overriding peer-group configuration.
598ce6bd
PM
5348 */
5349 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5350 /* Skip peers with overridden configuration. */
5351 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5352 PEER_FLAG_WEIGHT))
5353 continue;
5354
5355 /* Set flag and configuration on peer-group member. */
5356 SET_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT);
5357 if (member->weight[afi][safi] != weight) {
5358 member->weight[afi][safi] = weight;
5359 peer_on_policy_change(member, afi, safi, 0);
d62a17ae 5360 }
5361 }
598ce6bd 5362
d62a17ae 5363 return 0;
5364}
5365
5366int peer_weight_unset(struct peer *peer, afi_t afi, safi_t safi)
5367{
598ce6bd 5368 struct peer *member;
d62a17ae 5369 struct listnode *node, *nnode;
5370
598ce6bd
PM
5371 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_WEIGHT))
5372 return 0;
5373
5374 /* Inherit configuration from peer-group if peer is member. */
d62a17ae 5375 if (peer_group_active(peer)) {
598ce6bd 5376 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_WEIGHT);
e7103a96 5377 PEER_ATTR_INHERIT(peer, peer->group, weight[afi][safi]);
d62a17ae 5378
598ce6bd
PM
5379 peer_on_policy_change(peer, afi, safi, 0);
5380 return 0;
d62a17ae 5381 }
5382
598ce6bd
PM
5383 /* Remove flag and configuration from peer. */
5384 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_WEIGHT);
5385 peer->weight[afi][safi] = 0;
5386 peer_on_policy_change(peer, afi, safi, 0);
d62a17ae 5387
598ce6bd
PM
5388 /* Skip peer-group mechanics for regular peers. */
5389 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5390 return 0;
d62a17ae 5391
598ce6bd
PM
5392 /*
5393 * Remove flag and configuration from all peer-group members, unless
f75b9163 5394 * they are explicitly overriding peer-group configuration.
598ce6bd
PM
5395 */
5396 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5397 /* Skip peers with overridden configuration. */
5398 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5399 PEER_FLAG_WEIGHT))
5400 continue;
5401
5402 /* Skip peers where flag is already disabled. */
5403 if (!CHECK_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT))
5404 continue;
5405
5406 /* Remove flag and configuration on peer-group member. */
5407 UNSET_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT);
5408 member->weight[afi][safi] = 0;
5409 peer_on_policy_change(member, afi, safi, 0);
d62a17ae 5410 }
598ce6bd 5411
d62a17ae 5412 return 0;
5413}
5414
d7c0a89a 5415int peer_timers_set(struct peer *peer, uint32_t keepalive, uint32_t holdtime)
d62a17ae 5416{
b90a8e13 5417 struct peer *member;
d62a17ae 5418 struct listnode *node, *nnode;
5419
48e1932b 5420 if (keepalive > UINT16_MAX)
d62a17ae 5421 return BGP_ERR_INVALID_VALUE;
5422
48e1932b 5423 if (holdtime > UINT16_MAX)
d62a17ae 5424 return BGP_ERR_INVALID_VALUE;
5425
d62a17ae 5426 if (holdtime < 3 && holdtime != 0)
5427 return BGP_ERR_INVALID_VALUE;
5428
b90a8e13
PM
5429 /* Set flag and configuration on peer. */
5430 peer_flag_set(peer, PEER_FLAG_TIMER);
d62a17ae 5431 peer->holdtime = holdtime;
5432 peer->keepalive = (keepalive < holdtime / 3 ? keepalive : holdtime / 3);
5433
b90a8e13
PM
5434 /* Skip peer-group mechanics for regular peers. */
5435 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5436 return 0;
d25e4efc 5437
b90a8e13
PM
5438 /*
5439 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5440 * explicitly overriding peer-group configuration.
b90a8e13
PM
5441 */
5442 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5443 /* Skip peers with overridden configuration. */
5444 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER))
5445 continue;
d25e4efc 5446
b90a8e13
PM
5447 /* Set flag and configuration on peer-group member. */
5448 SET_FLAG(member->flags, PEER_FLAG_TIMER);
b28a12e6 5449 PEER_ATTR_INHERIT(member, peer->group, holdtime);
5450 PEER_ATTR_INHERIT(member, peer->group, keepalive);
d62a17ae 5451 }
d25e4efc 5452
d62a17ae 5453 return 0;
5454}
5455
5456int peer_timers_unset(struct peer *peer)
5457{
b90a8e13 5458 struct peer *member;
d62a17ae 5459 struct listnode *node, *nnode;
5460
b90a8e13
PM
5461 /* Inherit configuration from peer-group if peer is member. */
5462 if (peer_group_active(peer)) {
5463 peer_flag_inherit(peer, PEER_FLAG_TIMER);
5464 PEER_ATTR_INHERIT(peer, peer->group, holdtime);
5465 PEER_ATTR_INHERIT(peer, peer->group, keepalive);
5466 } else {
5467 /* Otherwise remove flag and configuration from peer. */
5468 peer_flag_unset(peer, PEER_FLAG_TIMER);
d25e4efc 5469 peer->holdtime = 0;
b90a8e13
PM
5470 peer->keepalive = 0;
5471 }
d25e4efc 5472
b90a8e13
PM
5473 /* Skip peer-group mechanics for regular peers. */
5474 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5475 return 0;
5476
5477 /*
5478 * Remove flag and configuration from all peer-group members, unless
f75b9163 5479 * they are explicitly overriding peer-group configuration.
b90a8e13
PM
5480 */
5481 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5482 /* Skip peers with overridden configuration. */
5483 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER))
5484 continue;
d25e4efc 5485
b90a8e13
PM
5486 /* Remove flag and configuration on peer-group member. */
5487 UNSET_FLAG(member->flags, PEER_FLAG_TIMER);
5488 member->holdtime = 0;
5489 member->keepalive = 0;
d62a17ae 5490 }
5491
5492 return 0;
5493}
5494
d7c0a89a 5495int peer_timers_connect_set(struct peer *peer, uint32_t connect)
d62a17ae 5496{
b90a8e13 5497 struct peer *member;
d62a17ae 5498 struct listnode *node, *nnode;
5499
48e1932b 5500 if (connect > UINT16_MAX)
d62a17ae 5501 return BGP_ERR_INVALID_VALUE;
5502
b90a8e13
PM
5503 /* Set flag and configuration on peer. */
5504 peer_flag_set(peer, PEER_FLAG_TIMER_CONNECT);
d62a17ae 5505 peer->connect = connect;
d62a17ae 5506 peer->v_connect = connect;
5507
b90a8e13 5508 /* Skip peer-group mechanics for regular peers. */
91de6fa2 5509 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
feb17238 5510 if (!peer_established(peer)) {
91de6fa2 5511 if (peer_active(peer))
5512 BGP_EVENT_ADD(peer, BGP_Stop);
5513 BGP_EVENT_ADD(peer, BGP_Start);
5514 }
d62a17ae 5515 return 0;
91de6fa2 5516 }
b90a8e13
PM
5517 /*
5518 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5519 * explicitly overriding peer-group configuration.
b90a8e13
PM
5520 */
5521 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5522 /* Skip peers with overridden configuration. */
5523 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER_CONNECT))
5524 continue;
5525
5526 /* Set flag and configuration on peer-group member. */
5527 SET_FLAG(member->flags, PEER_FLAG_TIMER_CONNECT);
5528 member->connect = connect;
5529 member->v_connect = connect;
91de6fa2 5530
feb17238 5531 if (!peer_established(member)) {
91de6fa2 5532 if (peer_active(member))
5533 BGP_EVENT_ADD(member, BGP_Stop);
5534 BGP_EVENT_ADD(member, BGP_Start);
5535 }
d62a17ae 5536 }
b90a8e13 5537
d62a17ae 5538 return 0;
5539}
5540
5541int peer_timers_connect_unset(struct peer *peer)
5542{
b90a8e13 5543 struct peer *member;
d62a17ae 5544 struct listnode *node, *nnode;
5545
b90a8e13
PM
5546 /* Inherit configuration from peer-group if peer is member. */
5547 if (peer_group_active(peer)) {
5548 peer_flag_inherit(peer, PEER_FLAG_TIMER_CONNECT);
5549 PEER_ATTR_INHERIT(peer, peer->group, connect);
5550 } else {
5551 /* Otherwise remove flag and configuration from peer. */
5552 peer_flag_unset(peer, PEER_FLAG_TIMER_CONNECT);
5553 peer->connect = 0;
5554 }
d62a17ae 5555
b90a8e13
PM
5556 /* Set timer with fallback to default value. */
5557 if (peer->connect)
5558 peer->v_connect = peer->connect;
5559 else
5d5393b9 5560 peer->v_connect = peer->bgp->default_connect_retry;
d62a17ae 5561
b90a8e13 5562 /* Skip peer-group mechanics for regular peers. */
91de6fa2 5563 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
feb17238 5564 if (!peer_established(peer)) {
91de6fa2 5565 if (peer_active(peer))
5566 BGP_EVENT_ADD(peer, BGP_Stop);
5567 BGP_EVENT_ADD(peer, BGP_Start);
5568 }
d62a17ae 5569 return 0;
91de6fa2 5570 }
b90a8e13
PM
5571 /*
5572 * Remove flag and configuration from all peer-group members, unless
f75b9163 5573 * they are explicitly overriding peer-group configuration.
b90a8e13
PM
5574 */
5575 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5576 /* Skip peers with overridden configuration. */
5577 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER_CONNECT))
5578 continue;
5579
5580 /* Remove flag and configuration on peer-group member. */
5581 UNSET_FLAG(member->flags, PEER_FLAG_TIMER_CONNECT);
5582 member->connect = 0;
5d5393b9 5583 member->v_connect = peer->bgp->default_connect_retry;
91de6fa2 5584
feb17238 5585 if (!peer_established(member)) {
91de6fa2 5586 if (peer_active(member))
5587 BGP_EVENT_ADD(member, BGP_Stop);
5588 BGP_EVENT_ADD(member, BGP_Start);
5589 }
d62a17ae 5590 }
b90a8e13 5591
d62a17ae 5592 return 0;
5593}
5594
d7c0a89a 5595int peer_advertise_interval_set(struct peer *peer, uint32_t routeadv)
d62a17ae 5596{
b90a8e13 5597 struct peer *member;
d62a17ae 5598 struct listnode *node, *nnode;
5599
5600 if (routeadv > 600)
5601 return BGP_ERR_INVALID_VALUE;
5602
b90a8e13
PM
5603 /* Set flag and configuration on peer. */
5604 peer_flag_set(peer, PEER_FLAG_ROUTEADV);
d62a17ae 5605 peer->routeadv = routeadv;
5606 peer->v_routeadv = routeadv;
5607
b90a8e13 5608 /* Check if handling a regular peer. */
d62a17ae 5609 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
b90a8e13 5610 /* Update peer route announcements. */
d62a17ae 5611 update_group_adjust_peer_afs(peer);
feb17238 5612 if (peer_established(peer))
d62a17ae 5613 bgp_announce_route_all(peer);
b90a8e13
PM
5614
5615 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5616 return 0;
5617 }
5618
b90a8e13
PM
5619 /*
5620 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5621 * explicitly overriding peer-group configuration.
b90a8e13
PM
5622 */
5623 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5624 /* Skip peers with overridden configuration. */
5625 if (CHECK_FLAG(member->flags_override, PEER_FLAG_ROUTEADV))
5626 continue;
5627
5628 /* Set flag and configuration on peer-group member. */
5629 SET_FLAG(member->flags, PEER_FLAG_ROUTEADV);
5630 member->routeadv = routeadv;
5631 member->v_routeadv = routeadv;
5632
5633 /* Update peer route announcements. */
5634 update_group_adjust_peer_afs(member);
feb17238 5635 if (peer_established(member))
b90a8e13 5636 bgp_announce_route_all(member);
d62a17ae 5637 }
5638
5639 return 0;
5640}
5641
5642int peer_advertise_interval_unset(struct peer *peer)
5643{
b90a8e13 5644 struct peer *member;
d62a17ae 5645 struct listnode *node, *nnode;
5646
b90a8e13
PM
5647 /* Inherit configuration from peer-group if peer is member. */
5648 if (peer_group_active(peer)) {
5649 peer_flag_inherit(peer, PEER_FLAG_ROUTEADV);
5650 PEER_ATTR_INHERIT(peer, peer->group, routeadv);
5651 } else {
5652 /* Otherwise remove flag and configuration from peer. */
5653 peer_flag_unset(peer, PEER_FLAG_ROUTEADV);
5654 peer->routeadv = 0;
5655 }
d62a17ae 5656
b90a8e13
PM
5657 /* Set timer with fallback to default value. */
5658 if (peer->routeadv)
5659 peer->v_routeadv = peer->routeadv;
d62a17ae 5660 else
b90a8e13
PM
5661 peer->v_routeadv = (peer->sort == BGP_PEER_IBGP)
5662 ? BGP_DEFAULT_IBGP_ROUTEADV
5663 : BGP_DEFAULT_EBGP_ROUTEADV;
d62a17ae 5664
b90a8e13 5665 /* Check if handling a regular peer. */
d62a17ae 5666 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
b90a8e13 5667 /* Update peer route announcements. */
d62a17ae 5668 update_group_adjust_peer_afs(peer);
feb17238 5669 if (peer_established(peer))
d62a17ae 5670 bgp_announce_route_all(peer);
b90a8e13
PM
5671
5672 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5673 return 0;
5674 }
5675
b90a8e13
PM
5676 /*
5677 * Remove flag and configuration from all peer-group members, unless
f75b9163 5678 * they are explicitly overriding peer-group configuration.
b90a8e13
PM
5679 */
5680 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5681 /* Skip peers with overridden configuration. */
5682 if (CHECK_FLAG(member->flags_override, PEER_FLAG_ROUTEADV))
5683 continue;
d62a17ae 5684
b90a8e13
PM
5685 /* Remove flag and configuration on peer-group member. */
5686 UNSET_FLAG(member->flags, PEER_FLAG_ROUTEADV);
5687 member->routeadv = 0;
5688 member->v_routeadv = (member->sort == BGP_PEER_IBGP)
5689 ? BGP_DEFAULT_IBGP_ROUTEADV
5690 : BGP_DEFAULT_EBGP_ROUTEADV;
d62a17ae 5691
b90a8e13
PM
5692 /* Update peer route announcements. */
5693 update_group_adjust_peer_afs(member);
feb17238 5694 if (peer_established(member))
b90a8e13 5695 bgp_announce_route_all(member);
d62a17ae 5696 }
5697
5698 return 0;
5699}
5700
6c537a18
DS
5701/* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5702 * interval
5703 */
5704int peer_timers_delayopen_set(struct peer *peer, uint32_t delayopen)
5705{
5706 struct peer *member;
5707 struct listnode *node;
5708
5709 /* Set peers session attribute flag and timer interval. */
5710 peer_flag_set(peer, PEER_FLAG_TIMER_DELAYOPEN);
5711 peer->delayopen = delayopen;
5712 peer->v_delayopen = delayopen;
5713
5714 /* Skip group mechanics for regular peers. */
5715 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5716 return 0;
5717
5718 /* Set flag and configuration on all peer-group members, unless they are
f75b9163 5719 * explicitly overriding peer-group configuration.
6c537a18
DS
5720 */
5721 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
5722 /* Skip peers with overridden configuration. */
5723 if (CHECK_FLAG(member->flags_override,
5724 PEER_FLAG_TIMER_DELAYOPEN))
5725 continue;
5726
5727 /* Set session attribute flag and timer intervals on peer-group
5728 * member.
5729 */
5730 SET_FLAG(member->flags, PEER_FLAG_TIMER_DELAYOPEN);
5731 member->delayopen = delayopen;
5732 member->v_delayopen = delayopen;
5733 }
5734
5735 return 0;
5736}
5737
5738/* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
5739 * DelayOpenTimer interval to the default value.
5740 */
5741int peer_timers_delayopen_unset(struct peer *peer)
5742{
5743 struct peer *member;
5744 struct listnode *node;
5745
5746 /* Inherit configuration from peer-group if peer is member. */
5747 if (peer_group_active(peer)) {
5748 peer_flag_inherit(peer, PEER_FLAG_TIMER_DELAYOPEN);
5749 PEER_ATTR_INHERIT(peer, peer->group, delayopen);
5750 } else {
5751 /* Otherwise remove session attribute flag and set timer
5752 * interval to default value.
5753 */
5754 peer_flag_unset(peer, PEER_FLAG_TIMER_DELAYOPEN);
5755 peer->delayopen = peer->bgp->default_delayopen;
5756 }
5757
5758 /* Set timer value to zero */
5759 peer->v_delayopen = 0;
5760
5761 /* Skip peer-group mechanics for regular peers. */
5762 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5763 return 0;
5764
5765 /* Remove flag and configuration from all peer-group members, unless
f75b9163 5766 * they are explicitly overriding peer-group configuration.
6c537a18
DS
5767 */
5768 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
5769 /* Skip peers with overridden configuration. */
5770 if (CHECK_FLAG(member->flags_override,
5771 PEER_FLAG_TIMER_DELAYOPEN))
5772 continue;
5773
5774 /* Remove session attribute flag, reset the timer interval to
5775 * the default value and set the timer value to zero.
5776 */
5777 UNSET_FLAG(member->flags, PEER_FLAG_TIMER_DELAYOPEN);
5778 member->delayopen = peer->bgp->default_delayopen;
5779 member->v_delayopen = 0;
5780 }
5781
5782 return 0;
5783}
5784
d62a17ae 5785/* neighbor interface */
5786void peer_interface_set(struct peer *peer, const char *str)
5787{
0a22ddfb 5788 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
d62a17ae 5789 peer->ifname = XSTRDUP(MTYPE_BGP_PEER_IFNAME, str);
5790}
5791
5792void peer_interface_unset(struct peer *peer)
5793{
0a22ddfb 5794 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
d62a17ae 5795}
5796
5797/* Allow-as in. */
5798int peer_allowas_in_set(struct peer *peer, afi_t afi, safi_t safi,
5799 int allow_num, int origin)
5800{
598ce6bd 5801 struct peer *member;
d62a17ae 5802 struct listnode *node, *nnode;
5803
598ce6bd
PM
5804 if (!origin && (allow_num < 1 || allow_num > 10))
5805 return BGP_ERR_INVALID_VALUE;
5806
5807 /* Set flag and configuration on peer. */
5808 peer_af_flag_set(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
d62a17ae 5809 if (origin) {
598ce6bd 5810 if (peer->allowas_in[afi][safi] != 0
d62a17ae 5811 || !CHECK_FLAG(peer->af_flags[afi][safi],
5812 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
d62a17ae 5813 peer_af_flag_set(peer, afi, safi,
5814 PEER_FLAG_ALLOWAS_IN_ORIGIN);
598ce6bd 5815 peer->allowas_in[afi][safi] = 0;
d62a17ae 5816 peer_on_policy_change(peer, afi, safi, 0);
5817 }
d62a17ae 5818 } else {
d62a17ae 5819 if (peer->allowas_in[afi][safi] != allow_num
5820 || CHECK_FLAG(peer->af_flags[afi][safi],
5821 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
598ce6bd 5822
d62a17ae 5823 peer_af_flag_unset(peer, afi, safi,
5824 PEER_FLAG_ALLOWAS_IN_ORIGIN);
598ce6bd 5825 peer->allowas_in[afi][safi] = allow_num;
d62a17ae 5826 peer_on_policy_change(peer, afi, safi, 0);
5827 }
598ce6bd 5828 }
d62a17ae 5829
598ce6bd
PM
5830 /* Skip peer-group mechanics for regular peers. */
5831 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5832 return 0;
d62a17ae 5833
598ce6bd
PM
5834 /*
5835 * Set flag and configuration on all peer-group members, unless
f75b9163 5836 * they are explicitly overriding peer-group configuration.
598ce6bd
PM
5837 */
5838 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5839 /* Skip peers with overridden configuration. */
5840 if (CHECK_FLAG(member->af_flags_override[afi][safi],
246bb5f0 5841 PEER_FLAG_ALLOWAS_IN))
598ce6bd
PM
5842 continue;
5843
5844 /* Set flag and configuration on peer-group member. */
5845 SET_FLAG(member->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
5846 if (origin) {
5847 if (member->allowas_in[afi][safi] != 0
5848 || !CHECK_FLAG(member->af_flags[afi][safi],
cf9ac8bf 5849 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
598ce6bd 5850 SET_FLAG(member->af_flags[afi][safi],
cf9ac8bf 5851 PEER_FLAG_ALLOWAS_IN_ORIGIN);
598ce6bd
PM
5852 member->allowas_in[afi][safi] = 0;
5853 peer_on_policy_change(peer, afi, safi, 0);
5854 }
5855 } else {
5856 if (member->allowas_in[afi][safi] != allow_num
5857 || CHECK_FLAG(member->af_flags[afi][safi],
d62a17ae 5858 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
598ce6bd
PM
5859 UNSET_FLAG(member->af_flags[afi][safi],
5860 PEER_FLAG_ALLOWAS_IN_ORIGIN);
5861 member->allowas_in[afi][safi] = allow_num;
d62a17ae 5862 peer_on_policy_change(peer, afi, safi, 0);
5863 }
5864 }
5865 }
5866
5867 return 0;
5868}
5869
5870int peer_allowas_in_unset(struct peer *peer, afi_t afi, safi_t safi)
5871{
598ce6bd 5872 struct peer *member;
d62a17ae 5873 struct listnode *node, *nnode;
5874
598ce6bd
PM
5875 /* Skip peer if flag is already disabled. */
5876 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
5877 return 0;
d62a17ae 5878
598ce6bd
PM
5879 /* Inherit configuration from peer-group if peer is member. */
5880 if (peer_group_active(peer)) {
5881 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
5882 peer_af_flag_inherit(peer, afi, safi,
cf9ac8bf 5883 PEER_FLAG_ALLOWAS_IN_ORIGIN);
e7103a96 5884 PEER_ATTR_INHERIT(peer, peer->group, allowas_in[afi][safi]);
d62a17ae 5885 peer_on_policy_change(peer, afi, safi, 0);
598ce6bd
PM
5886
5887 return 0;
5888 }
5889
5890 /* Remove flag and configuration from peer. */
5891 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
5892 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_ALLOWAS_IN_ORIGIN);
5893 peer->allowas_in[afi][safi] = 0;
5894 peer_on_policy_change(peer, afi, safi, 0);
5895
5896 /* Skip peer-group mechanics if handling a regular peer. */
5897 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5898 return 0;
5899
5900 /*
5901 * Remove flags and configuration from all peer-group members, unless
f75b9163 5902 * they are explicitly overriding peer-group configuration.
598ce6bd
PM
5903 */
5904 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5905 /* Skip peers with overridden configuration. */
5906 if (CHECK_FLAG(member->af_flags_override[afi][safi],
cf9ac8bf 5907 PEER_FLAG_ALLOWAS_IN))
598ce6bd
PM
5908 continue;
5909
598ce6bd 5910 /* Remove flags and configuration on peer-group member. */
cf9ac8bf 5911 UNSET_FLAG(member->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
598ce6bd 5912 UNSET_FLAG(member->af_flags[afi][safi],
cf9ac8bf 5913 PEER_FLAG_ALLOWAS_IN_ORIGIN);
598ce6bd
PM
5914 member->allowas_in[afi][safi] = 0;
5915 peer_on_policy_change(member, afi, safi, 0);
d62a17ae 5916 }
5917
5918 return 0;
5919}
5920
f4b8ec07
CS
5921int peer_local_as_set(struct peer *peer, as_t as, bool no_prepend,
5922 bool replace_as)
d62a17ae 5923{
a14810f4 5924 bool old_no_prepend, old_replace_as;
d62a17ae 5925 struct bgp *bgp = peer->bgp;
a14810f4 5926 struct peer *member;
d62a17ae 5927 struct listnode *node, *nnode;
3061acc2 5928 enum bgp_peer_sort ptype = peer_sort(peer);
d62a17ae 5929
469cbb0f 5930 if (ptype != BGP_PEER_EBGP && ptype != BGP_PEER_INTERNAL)
d62a17ae 5931 return BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP;
5932
5933 if (bgp->as == as)
5934 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS;
5935
5936 if (peer->as == as)
5937 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS_REMOTE_AS;
5938
a14810f4
PM
5939 /* Save previous flag states. */
5940 old_no_prepend =
5941 !!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
5942 old_replace_as =
5943 !!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
d62a17ae 5944
a14810f4
PM
5945 /* Set flag and configuration on peer. */
5946 peer_flag_set(peer, PEER_FLAG_LOCAL_AS);
5947 peer_flag_modify(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND, no_prepend);
5948 peer_flag_modify(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS, replace_as);
d62a17ae 5949
a14810f4
PM
5950 if (peer->change_local_as == as && old_no_prepend == no_prepend
5951 && old_replace_as == replace_as)
5952 return 0;
5953 peer->change_local_as = as;
d62a17ae 5954
a14810f4 5955 /* Check if handling a regular peer. */
d62a17ae 5956 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 5957 /* Send notification or reset peer depending on state. */
d62a17ae 5958 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5959 peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
5960 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5961 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5962 } else
5963 bgp_session_reset(peer);
a14810f4
PM
5964
5965 /* Skip peer-group mechanics for regular peers. */
d62a17ae 5966 return 0;
5967 }
5968
a14810f4
PM
5969 /*
5970 * Set flag and configuration on all peer-group members, unless they are
f75b9163 5971 * explicitly overriding peer-group configuration.
a14810f4
PM
5972 */
5973 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5974 /* Skip peers with overridden configuration. */
5975 if (CHECK_FLAG(member->flags_override, PEER_FLAG_LOCAL_AS))
5976 continue;
d62a17ae 5977
a14810f4
PM
5978 /* Skip peers with the same configuration. */
5979 old_no_prepend = CHECK_FLAG(member->flags,
5980 PEER_FLAG_LOCAL_AS_NO_PREPEND);
5981 old_replace_as = CHECK_FLAG(member->flags,
5982 PEER_FLAG_LOCAL_AS_REPLACE_AS);
5983 if (member->change_local_as == as
5984 && CHECK_FLAG(member->flags, PEER_FLAG_LOCAL_AS)
5985 && old_no_prepend == no_prepend
5986 && old_replace_as == replace_as)
5987 continue;
d62a17ae 5988
a14810f4
PM
5989 /* Set flag and configuration on peer-group member. */
5990 SET_FLAG(member->flags, PEER_FLAG_LOCAL_AS);
5991 COND_FLAG(member->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND,
5992 no_prepend);
5993 COND_FLAG(member->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS,
5994 replace_as);
5995 member->change_local_as = as;
5996
5997 /* Send notification or stop peer depending on state. */
5998 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5999 member->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6000 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 6001 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6002 } else
a14810f4 6003 BGP_EVENT_ADD(member, BGP_Stop);
d62a17ae 6004 }
6005
6006 return 0;
6007}
6008
6009int peer_local_as_unset(struct peer *peer)
6010{
a14810f4 6011 struct peer *member;
d62a17ae 6012 struct listnode *node, *nnode;
6013
a14810f4 6014 if (!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS))
d62a17ae 6015 return 0;
6016
a14810f4
PM
6017 /* Inherit configuration from peer-group if peer is member. */
6018 if (peer_group_active(peer)) {
6019 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS);
6020 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6021 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6022 PEER_ATTR_INHERIT(peer, peer->group, change_local_as);
6023 } else {
6024 /* Otherwise remove flag and configuration from peer. */
6025 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS);
6026 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6027 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6028 peer->change_local_as = 0;
6029 }
d62a17ae 6030
a14810f4 6031 /* Check if handling a regular peer. */
d62a17ae 6032 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 6033 /* Send notification or stop peer depending on state. */
d62a17ae 6034 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
6035 peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6036 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6037 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6038 } else
6039 BGP_EVENT_ADD(peer, BGP_Stop);
6040
a14810f4 6041 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6042 return 0;
6043 }
6044
a14810f4
PM
6045 /*
6046 * Remove flag and configuration from all peer-group members, unless
f75b9163 6047 * they are explicitly overriding peer-group configuration.
a14810f4
PM
6048 */
6049 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6050 /* Skip peers with overridden configuration. */
6051 if (CHECK_FLAG(member->flags_override, PEER_FLAG_LOCAL_AS))
6052 continue;
d62a17ae 6053
a14810f4
PM
6054 /* Remove flag and configuration on peer-group member. */
6055 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS);
6056 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6057 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6058 member->change_local_as = 0;
6059
6060 /* Send notification or stop peer depending on state. */
6061 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
6062 member->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6063 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 6064 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6065 } else
a14810f4 6066 bgp_session_reset(member);
d62a17ae 6067 }
a14810f4 6068
d62a17ae 6069 return 0;
6070}
6071
6072/* Set password for authenticating with the peer. */
6073int peer_password_set(struct peer *peer, const char *password)
6074{
a14810f4
PM
6075 struct peer *member;
6076 struct listnode *node, *nnode;
d62a17ae 6077 int len = password ? strlen(password) : 0;
6078 int ret = BGP_SUCCESS;
6079
6080 if ((len < PEER_PASSWORD_MINLEN) || (len > PEER_PASSWORD_MAXLEN))
6081 return BGP_ERR_INVALID_VALUE;
6082
a14810f4
PM
6083 /* Set flag and configuration on peer. */
6084 peer_flag_set(peer, PEER_FLAG_PASSWORD);
6085 if (peer->password && strcmp(peer->password, password) == 0)
d62a17ae 6086 return 0;
a14810f4 6087 XFREE(MTYPE_PEER_PASSWORD, peer->password);
d62a17ae 6088 peer->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
6089
a14810f4 6090 /* Check if handling a regular peer. */
d62a17ae 6091 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 6092 /* Send notification or reset peer depending on state. */
d62a17ae 6093 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
6094 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6095 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6096 else
6097 bgp_session_reset(peer);
6098
a14810f4
PM
6099 /*
6100 * Attempt to install password on socket and skip peer-group
6101 * mechanics.
6102 */
d62a17ae 6103 if (BGP_PEER_SU_UNSPEC(peer))
6104 return BGP_SUCCESS;
d62a17ae 6105 return (bgp_md5_set(peer) >= 0) ? BGP_SUCCESS
6106 : BGP_ERR_TCPSIG_FAILED;
6107 }
6108
a14810f4
PM
6109 /*
6110 * Set flag and configuration on all peer-group members, unless they are
f75b9163 6111 * explicitly overriding peer-group configuration.
a14810f4
PM
6112 */
6113 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6114 /* Skip peers with overridden configuration. */
6115 if (CHECK_FLAG(member->flags_override, PEER_FLAG_PASSWORD))
d62a17ae 6116 continue;
6117
a14810f4
PM
6118 /* Skip peers with the same password. */
6119 if (member->password && strcmp(member->password, password) == 0)
6120 continue;
d62a17ae 6121
a14810f4
PM
6122 /* Set flag and configuration on peer-group member. */
6123 SET_FLAG(member->flags, PEER_FLAG_PASSWORD);
6124 if (member->password)
6125 XFREE(MTYPE_PEER_PASSWORD, member->password);
6126 member->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
6127
6128 /* Send notification or reset peer depending on state. */
6129 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status))
6130 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 6131 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6132 else
a14810f4 6133 bgp_session_reset(member);
d62a17ae 6134
a14810f4
PM
6135 /* Attempt to install password on socket. */
6136 if (!BGP_PEER_SU_UNSPEC(member) && bgp_md5_set(member) < 0)
6137 ret = BGP_ERR_TCPSIG_FAILED;
d62a17ae 6138 }
6139
9e7d9a61
QY
6140 /* Set flag and configuration on all peer-group listen ranges */
6141 struct listnode *ln;
6142 struct prefix *lr;
6143
6144 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP], ln, lr))
a4faae3a 6145 bgp_md5_set_prefix(peer->bgp, lr, password);
9e7d9a61 6146 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP6], ln, lr))
a4faae3a 6147 bgp_md5_set_prefix(peer->bgp, lr, password);
9e7d9a61 6148
d62a17ae 6149 return ret;
6150}
6151
6152int peer_password_unset(struct peer *peer)
6153{
a14810f4
PM
6154 struct peer *member;
6155 struct listnode *node, *nnode;
d62a17ae 6156
a14810f4 6157 if (!CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD))
d62a17ae 6158 return 0;
6159
a14810f4
PM
6160 /* Inherit configuration from peer-group if peer is member. */
6161 if (peer_group_active(peer)) {
6162 peer_flag_inherit(peer, PEER_FLAG_PASSWORD);
6163 PEER_STR_ATTR_INHERIT(peer, peer->group, password,
6164 MTYPE_PEER_PASSWORD);
6165 } else {
6166 /* Otherwise remove flag and configuration from peer. */
6167 peer_flag_unset(peer, PEER_FLAG_PASSWORD);
6168 XFREE(MTYPE_PEER_PASSWORD, peer->password);
6169 }
6170
6171 /* Check if handling a regular peer. */
d62a17ae 6172 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
a14810f4 6173 /* Send notification or reset peer depending on state. */
d62a17ae 6174 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
6175 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6176 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6177 else
6178 bgp_session_reset(peer);
6179
a14810f4 6180 /* Attempt to uninstall password on socket. */
d62a17ae 6181 if (!BGP_PEER_SU_UNSPEC(peer))
6182 bgp_md5_unset(peer);
a14810f4 6183 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6184 return 0;
6185 }
6186
a14810f4
PM
6187 /*
6188 * Remove flag and configuration from all peer-group members, unless
f75b9163 6189 * they are explicitly overriding peer-group configuration.
a14810f4
PM
6190 */
6191 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6192 /* Skip peers with overridden configuration. */
6193 if (CHECK_FLAG(member->flags_override, PEER_FLAG_PASSWORD))
d62a17ae 6194 continue;
6195
a14810f4
PM
6196 /* Remove flag and configuration on peer-group member. */
6197 UNSET_FLAG(member->flags, PEER_FLAG_PASSWORD);
6198 XFREE(MTYPE_PEER_PASSWORD, member->password);
6199
6200 /* Send notification or reset peer depending on state. */
6201 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status))
6202 bgp_notify_send(member, BGP_NOTIFY_CEASE,
d62a17ae 6203 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6204 else
a14810f4 6205 bgp_session_reset(member);
d62a17ae 6206
a14810f4
PM
6207 /* Attempt to uninstall password on socket. */
6208 if (!BGP_PEER_SU_UNSPEC(member))
6209 bgp_md5_unset(member);
d62a17ae 6210 }
6211
9e7d9a61
QY
6212 /* Set flag and configuration on all peer-group listen ranges */
6213 struct listnode *ln;
6214 struct prefix *lr;
6215
6216 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP], ln, lr))
a4faae3a 6217 bgp_md5_unset_prefix(peer->bgp, lr);
9e7d9a61 6218 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP6], ln, lr))
a4faae3a 6219 bgp_md5_unset_prefix(peer->bgp, lr);
9e7d9a61 6220
d62a17ae 6221 return 0;
6222}
6b0655a2 6223
3f9c7369 6224
718e3744 6225/* Set distribute list to the peer. */
d62a17ae 6226int peer_distribute_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6227 const char *name)
6228{
70ee29b4 6229 struct peer *member;
d62a17ae 6230 struct bgp_filter *filter;
d62a17ae 6231 struct listnode *node, *nnode;
6232
6233 if (direct != FILTER_IN && direct != FILTER_OUT)
6234 return BGP_ERR_INVALID_VALUE;
6235
70ee29b4 6236 /* Set configuration on peer. */
d62a17ae 6237 filter = &peer->filter[afi][safi];
d62a17ae 6238 if (filter->plist[direct].name)
6239 return BGP_ERR_PEER_FILTER_CONFLICT;
d62a17ae 6240 if (filter->dlist[direct].name)
6241 XFREE(MTYPE_BGP_FILTER_NAME, filter->dlist[direct].name);
6242 filter->dlist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6243 filter->dlist[direct].alist = access_list_lookup(afi, name);
6244
70ee29b4 6245 /* Check if handling a regular peer. */
d62a17ae 6246 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4
PM
6247 /* Set override-flag and process peer route updates. */
6248 SET_FLAG(peer->filter_override[afi][safi][direct],
6249 PEER_FT_DISTRIBUTE_LIST);
d62a17ae 6250 peer_on_policy_change(peer, afi, safi,
6251 (direct == FILTER_OUT) ? 1 : 0);
70ee29b4
PM
6252
6253 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6254 return 0;
6255 }
6256
70ee29b4
PM
6257 /*
6258 * Set configuration on all peer-group members, un less they are
f75b9163 6259 * explicitly overriding peer-group configuration.
70ee29b4
PM
6260 */
6261 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6262 /* Skip peers with overridden configuration. */
6263 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6264 PEER_FT_DISTRIBUTE_LIST))
6265 continue;
d62a17ae 6266
70ee29b4
PM
6267 /* Set configuration on peer-group member. */
6268 filter = &member->filter[afi][safi];
d62a17ae 6269 if (filter->dlist[direct].name)
6270 XFREE(MTYPE_BGP_FILTER_NAME,
6271 filter->dlist[direct].name);
6272 filter->dlist[direct].name =
6273 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6274 filter->dlist[direct].alist = access_list_lookup(afi, name);
70ee29b4
PM
6275
6276 /* Process peer route updates. */
6277 peer_on_policy_change(member, afi, safi,
d62a17ae 6278 (direct == FILTER_OUT) ? 1 : 0);
6279 }
6280
6281 return 0;
6282}
6283
6284int peer_distribute_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6285{
70ee29b4 6286 struct peer *member;
d62a17ae 6287 struct bgp_filter *filter;
d62a17ae 6288 struct listnode *node, *nnode;
6289
6290 if (direct != FILTER_IN && direct != FILTER_OUT)
6291 return BGP_ERR_INVALID_VALUE;
6292
70ee29b4
PM
6293 /* Unset override-flag unconditionally. */
6294 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6295 PEER_FT_DISTRIBUTE_LIST);
d62a17ae 6296
70ee29b4 6297 /* Inherit configuration from peer-group if peer is member. */
d62a17ae 6298 if (peer_group_active(peer)) {
e7103a96
PM
6299 PEER_STR_ATTR_INHERIT(peer, peer->group,
6300 filter[afi][safi].dlist[direct].name,
6301 MTYPE_BGP_FILTER_NAME);
6302 PEER_ATTR_INHERIT(peer, peer->group,
6303 filter[afi][safi].dlist[direct].alist);
70ee29b4
PM
6304 } else {
6305 /* Otherwise remove configuration from peer. */
6306 filter = &peer->filter[afi][safi];
6307 if (filter->dlist[direct].name)
6308 XFREE(MTYPE_BGP_FILTER_NAME,
6309 filter->dlist[direct].name);
6310 filter->dlist[direct].name = NULL;
6311 filter->dlist[direct].alist = NULL;
d62a17ae 6312 }
6313
70ee29b4 6314 /* Check if handling a regular peer. */
d62a17ae 6315 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4 6316 /* Process peer route updates. */
d62a17ae 6317 peer_on_policy_change(peer, afi, safi,
6318 (direct == FILTER_OUT) ? 1 : 0);
70ee29b4
PM
6319
6320 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6321 return 0;
6322 }
6323
70ee29b4
PM
6324 /*
6325 * Remove configuration on all peer-group members, unless they are
f75b9163 6326 * explicitly overriding peer-group configuration.
70ee29b4
PM
6327 */
6328 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6329 /* Skip peers with overridden configuration. */
6330 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6331 PEER_FT_DISTRIBUTE_LIST))
6332 continue;
d62a17ae 6333
70ee29b4
PM
6334 /* Remove configuration on peer-group member. */
6335 filter = &member->filter[afi][safi];
d62a17ae 6336 if (filter->dlist[direct].name)
6337 XFREE(MTYPE_BGP_FILTER_NAME,
6338 filter->dlist[direct].name);
6339 filter->dlist[direct].name = NULL;
6340 filter->dlist[direct].alist = NULL;
70ee29b4
PM
6341
6342 /* Process peer route updates. */
6343 peer_on_policy_change(member, afi, safi,
d62a17ae 6344 (direct == FILTER_OUT) ? 1 : 0);
6345 }
6346
6347 return 0;
6348}
6349
6350/* Update distribute list. */
6351static void peer_distribute_update(struct access_list *access)
6352{
6353 afi_t afi;
6354 safi_t safi;
6355 int direct;
6356 struct listnode *mnode, *mnnode;
6357 struct listnode *node, *nnode;
6358 struct bgp *bgp;
6359 struct peer *peer;
6360 struct peer_group *group;
6361 struct bgp_filter *filter;
6362
6363 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6364 if (access->name)
b0a007df
IR
6365 update_group_policy_update(bgp,
6366 BGP_POLICY_DISTRIBUTE_LIST,
d62a17ae 6367 access->name, 0, 0);
6368 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
05c7a1cc
QY
6369 FOREACH_AFI_SAFI (afi, safi) {
6370 filter = &peer->filter[afi][safi];
6371
6372 for (direct = FILTER_IN; direct < FILTER_MAX;
6373 direct++) {
6374 if (filter->dlist[direct].name)
6375 filter->dlist[direct]
6376 .alist = access_list_lookup(
6377 afi,
d62a17ae 6378 filter->dlist[direct]
05c7a1cc
QY
6379 .name);
6380 else
6381 filter->dlist[direct].alist =
6382 NULL;
d62a17ae 6383 }
05c7a1cc 6384 }
d62a17ae 6385 }
6386 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
05c7a1cc
QY
6387 FOREACH_AFI_SAFI (afi, safi) {
6388 filter = &group->conf->filter[afi][safi];
6389
6390 for (direct = FILTER_IN; direct < FILTER_MAX;
6391 direct++) {
6392 if (filter->dlist[direct].name)
6393 filter->dlist[direct]
6394 .alist = access_list_lookup(
6395 afi,
d62a17ae 6396 filter->dlist[direct]
05c7a1cc
QY
6397 .name);
6398 else
6399 filter->dlist[direct].alist =
6400 NULL;
d62a17ae 6401 }
05c7a1cc 6402 }
d62a17ae 6403 }
49e5a4a0 6404#ifdef ENABLE_BGP_VNC
d62a17ae 6405 vnc_prefix_list_update(bgp);
6406#endif
6407 }
6408}
6409
6410/* Set prefix list to the peer. */
6411int peer_prefix_list_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6412 const char *name)
6413{
70ee29b4 6414 struct peer *member;
d62a17ae 6415 struct bgp_filter *filter;
d62a17ae 6416 struct listnode *node, *nnode;
6417
6418 if (direct != FILTER_IN && direct != FILTER_OUT)
6419 return BGP_ERR_INVALID_VALUE;
6420
70ee29b4 6421 /* Set configuration on peer. */
d62a17ae 6422 filter = &peer->filter[afi][safi];
d62a17ae 6423 if (filter->dlist[direct].name)
6424 return BGP_ERR_PEER_FILTER_CONFLICT;
d62a17ae 6425 if (filter->plist[direct].name)
6426 XFREE(MTYPE_BGP_FILTER_NAME, filter->plist[direct].name);
6427 filter->plist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6428 filter->plist[direct].plist = prefix_list_lookup(afi, name);
6429
70ee29b4 6430 /* Check if handling a regular peer. */
d62a17ae 6431 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4
PM
6432 /* Set override-flag and process peer route updates. */
6433 SET_FLAG(peer->filter_override[afi][safi][direct],
6434 PEER_FT_PREFIX_LIST);
d62a17ae 6435 peer_on_policy_change(peer, afi, safi,
6436 (direct == FILTER_OUT) ? 1 : 0);
70ee29b4
PM
6437
6438 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6439 return 0;
6440 }
6441
70ee29b4
PM
6442 /*
6443 * Set configuration on all peer-group members, unless they are
f75b9163 6444 * explicitly overriding peer-group configuration.
70ee29b4
PM
6445 */
6446 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6447 /* Skip peers with overridden configuration. */
6448 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6449 PEER_FT_PREFIX_LIST))
6450 continue;
d62a17ae 6451
70ee29b4
PM
6452 /* Set configuration on peer-group member. */
6453 filter = &member->filter[afi][safi];
d62a17ae 6454 if (filter->plist[direct].name)
6455 XFREE(MTYPE_BGP_FILTER_NAME,
6456 filter->plist[direct].name);
6457 filter->plist[direct].name =
6458 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6459 filter->plist[direct].plist = prefix_list_lookup(afi, name);
70ee29b4
PM
6460
6461 /* Process peer route updates. */
6462 peer_on_policy_change(member, afi, safi,
d62a17ae 6463 (direct == FILTER_OUT) ? 1 : 0);
6464 }
70ee29b4 6465
d62a17ae 6466 return 0;
6467}
6468
6469int peer_prefix_list_unset(struct peer *peer, afi_t afi, safi_t safi,
6470 int direct)
6471{
70ee29b4 6472 struct peer *member;
d62a17ae 6473 struct bgp_filter *filter;
d62a17ae 6474 struct listnode *node, *nnode;
6475
6476 if (direct != FILTER_IN && direct != FILTER_OUT)
6477 return BGP_ERR_INVALID_VALUE;
6478
70ee29b4
PM
6479 /* Unset override-flag unconditionally. */
6480 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6481 PEER_FT_PREFIX_LIST);
d62a17ae 6482
70ee29b4 6483 /* Inherit configuration from peer-group if peer is member. */
d62a17ae 6484 if (peer_group_active(peer)) {
e7103a96
PM
6485 PEER_STR_ATTR_INHERIT(peer, peer->group,
6486 filter[afi][safi].plist[direct].name,
6487 MTYPE_BGP_FILTER_NAME);
6488 PEER_ATTR_INHERIT(peer, peer->group,
6489 filter[afi][safi].plist[direct].plist);
70ee29b4
PM
6490 } else {
6491 /* Otherwise remove configuration from peer. */
6492 filter = &peer->filter[afi][safi];
6493 if (filter->plist[direct].name)
6494 XFREE(MTYPE_BGP_FILTER_NAME,
6495 filter->plist[direct].name);
6496 filter->plist[direct].name = NULL;
6497 filter->plist[direct].plist = NULL;
d62a17ae 6498 }
6499
70ee29b4 6500 /* Check if handling a regular peer. */
d62a17ae 6501 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4 6502 /* Process peer route updates. */
d62a17ae 6503 peer_on_policy_change(peer, afi, safi,
6504 (direct == FILTER_OUT) ? 1 : 0);
70ee29b4
PM
6505
6506 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6507 return 0;
6508 }
6509
70ee29b4
PM
6510 /*
6511 * Remove configuration on all peer-group members, unless they are
f75b9163 6512 * explicitly overriding peer-group configuration.
70ee29b4
PM
6513 */
6514 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6515 /* Skip peers with overridden configuration. */
6516 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6517 PEER_FT_PREFIX_LIST))
6518 continue;
d62a17ae 6519
70ee29b4
PM
6520 /* Remove configuration on peer-group member. */
6521 filter = &member->filter[afi][safi];
d62a17ae 6522 if (filter->plist[direct].name)
6523 XFREE(MTYPE_BGP_FILTER_NAME,
6524 filter->plist[direct].name);
6525 filter->plist[direct].name = NULL;
6526 filter->plist[direct].plist = NULL;
70ee29b4
PM
6527
6528 /* Process peer route updates. */
6529 peer_on_policy_change(member, afi, safi,
d62a17ae 6530 (direct == FILTER_OUT) ? 1 : 0);
6531 }
6532
6533 return 0;
6534}
6535
6536/* Update prefix-list list. */
6537static void peer_prefix_list_update(struct prefix_list *plist)
6538{
6539 struct listnode *mnode, *mnnode;
6540 struct listnode *node, *nnode;
6541 struct bgp *bgp;
6542 struct peer *peer;
6543 struct peer_group *group;
6544 struct bgp_filter *filter;
6545 afi_t afi;
6546 safi_t safi;
6547 int direct;
6548
6549 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6550
6551 /*
6552 * Update the prefix-list on update groups.
6553 */
6554 update_group_policy_update(
6555 bgp, BGP_POLICY_PREFIX_LIST,
6556 plist ? prefix_list_name(plist) : NULL, 0, 0);
6557
6558 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
05c7a1cc
QY
6559 FOREACH_AFI_SAFI (afi, safi) {
6560 filter = &peer->filter[afi][safi];
6561
6562 for (direct = FILTER_IN; direct < FILTER_MAX;
6563 direct++) {
6564 if (filter->plist[direct].name)
6565 filter->plist[direct]
6566 .plist = prefix_list_lookup(
6567 afi,
d62a17ae 6568 filter->plist[direct]
05c7a1cc
QY
6569 .name);
6570 else
6571 filter->plist[direct].plist =
6572 NULL;
d62a17ae 6573 }
05c7a1cc 6574 }
d62a17ae 6575 }
6576 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
05c7a1cc
QY
6577 FOREACH_AFI_SAFI (afi, safi) {
6578 filter = &group->conf->filter[afi][safi];
6579
6580 for (direct = FILTER_IN; direct < FILTER_MAX;
6581 direct++) {
6582 if (filter->plist[direct].name)
6583 filter->plist[direct]
6584 .plist = prefix_list_lookup(
6585 afi,
d62a17ae 6586 filter->plist[direct]
05c7a1cc
QY
6587 .name);
6588 else
6589 filter->plist[direct].plist =
6590 NULL;
d62a17ae 6591 }
05c7a1cc 6592 }
d62a17ae 6593 }
6594 }
6595}
6596
6597int peer_aslist_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6598 const char *name)
6599{
70ee29b4 6600 struct peer *member;
d62a17ae 6601 struct bgp_filter *filter;
d62a17ae 6602 struct listnode *node, *nnode;
6603
6604 if (direct != FILTER_IN && direct != FILTER_OUT)
6605 return BGP_ERR_INVALID_VALUE;
6606
70ee29b4 6607 /* Set configuration on peer. */
d62a17ae 6608 filter = &peer->filter[afi][safi];
d62a17ae 6609 if (filter->aslist[direct].name)
6610 XFREE(MTYPE_BGP_FILTER_NAME, filter->aslist[direct].name);
6611 filter->aslist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6612 filter->aslist[direct].aslist = as_list_lookup(name);
6613
70ee29b4 6614 /* Check if handling a regular peer. */
d62a17ae 6615 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4
PM
6616 /* Set override-flag and process peer route updates. */
6617 SET_FLAG(peer->filter_override[afi][safi][direct],
6618 PEER_FT_FILTER_LIST);
d62a17ae 6619 peer_on_policy_change(peer, afi, safi,
6620 (direct == FILTER_OUT) ? 1 : 0);
70ee29b4
PM
6621
6622 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6623 return 0;
6624 }
6625
70ee29b4
PM
6626 /*
6627 * Set configuration on all peer-group members, unless they are
f75b9163 6628 * explicitly overriding peer-group configuration.
70ee29b4
PM
6629 */
6630 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6631 /* Skip peers with overridden configuration. */
6632 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6633 PEER_FT_FILTER_LIST))
6634 continue;
d62a17ae 6635
70ee29b4
PM
6636 /* Set configuration on peer-group member. */
6637 filter = &member->filter[afi][safi];
d62a17ae 6638 if (filter->aslist[direct].name)
6639 XFREE(MTYPE_BGP_FILTER_NAME,
6640 filter->aslist[direct].name);
6641 filter->aslist[direct].name =
6642 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6643 filter->aslist[direct].aslist = as_list_lookup(name);
70ee29b4
PM
6644
6645 /* Process peer route updates. */
6646 peer_on_policy_change(member, afi, safi,
d62a17ae 6647 (direct == FILTER_OUT) ? 1 : 0);
6648 }
70ee29b4 6649
d62a17ae 6650 return 0;
6651}
6652
6653int peer_aslist_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6654{
70ee29b4 6655 struct peer *member;
d62a17ae 6656 struct bgp_filter *filter;
d62a17ae 6657 struct listnode *node, *nnode;
6658
6659 if (direct != FILTER_IN && direct != FILTER_OUT)
6660 return BGP_ERR_INVALID_VALUE;
6661
70ee29b4
PM
6662 /* Unset override-flag unconditionally. */
6663 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6664 PEER_FT_FILTER_LIST);
d62a17ae 6665
70ee29b4 6666 /* Inherit configuration from peer-group if peer is member. */
d62a17ae 6667 if (peer_group_active(peer)) {
e7103a96
PM
6668 PEER_STR_ATTR_INHERIT(peer, peer->group,
6669 filter[afi][safi].aslist[direct].name,
6670 MTYPE_BGP_FILTER_NAME);
6671 PEER_ATTR_INHERIT(peer, peer->group,
70ee29b4
PM
6672 filter[afi][safi].aslist[direct].aslist);
6673 } else {
6674 /* Otherwise remove configuration from peer. */
6675 filter = &peer->filter[afi][safi];
6676 if (filter->aslist[direct].name)
6677 XFREE(MTYPE_BGP_FILTER_NAME,
6678 filter->aslist[direct].name);
6679 filter->aslist[direct].name = NULL;
6680 filter->aslist[direct].aslist = NULL;
d62a17ae 6681 }
6682
70ee29b4 6683 /* Check if handling a regular peer. */
d62a17ae 6684 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4 6685 /* Process peer route updates. */
d62a17ae 6686 peer_on_policy_change(peer, afi, safi,
6687 (direct == FILTER_OUT) ? 1 : 0);
70ee29b4
PM
6688
6689 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6690 return 0;
6691 }
6692
70ee29b4
PM
6693 /*
6694 * Remove configuration on all peer-group members, unless they are
f75b9163 6695 * explicitly overriding peer-group configuration.
70ee29b4
PM
6696 */
6697 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6698 /* Skip peers with overridden configuration. */
6699 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6700 PEER_FT_FILTER_LIST))
6701 continue;
d62a17ae 6702
70ee29b4
PM
6703 /* Remove configuration on peer-group member. */
6704 filter = &member->filter[afi][safi];
d62a17ae 6705 if (filter->aslist[direct].name)
6706 XFREE(MTYPE_BGP_FILTER_NAME,
6707 filter->aslist[direct].name);
6708 filter->aslist[direct].name = NULL;
6709 filter->aslist[direct].aslist = NULL;
70ee29b4
PM
6710
6711 /* Process peer route updates. */
6712 peer_on_policy_change(member, afi, safi,
d62a17ae 6713 (direct == FILTER_OUT) ? 1 : 0);
6714 }
6715
6716 return 0;
6717}
6718
6719static void peer_aslist_update(const char *aslist_name)
6720{
6721 afi_t afi;
6722 safi_t safi;
6723 int direct;
6724 struct listnode *mnode, *mnnode;
6725 struct listnode *node, *nnode;
6726 struct bgp *bgp;
6727 struct peer *peer;
6728 struct peer_group *group;
6729 struct bgp_filter *filter;
6730
6731 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6732 update_group_policy_update(bgp, BGP_POLICY_FILTER_LIST,
6733 aslist_name, 0, 0);
6734
6735 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
05c7a1cc
QY
6736 FOREACH_AFI_SAFI (afi, safi) {
6737 filter = &peer->filter[afi][safi];
6738
6739 for (direct = FILTER_IN; direct < FILTER_MAX;
6740 direct++) {
6741 if (filter->aslist[direct].name)
6742 filter->aslist[direct]
6743 .aslist = as_list_lookup(
d62a17ae 6744 filter->aslist[direct]
05c7a1cc
QY
6745 .name);
6746 else
6747 filter->aslist[direct].aslist =
6748 NULL;
d62a17ae 6749 }
05c7a1cc 6750 }
d62a17ae 6751 }
6752 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
05c7a1cc
QY
6753 FOREACH_AFI_SAFI (afi, safi) {
6754 filter = &group->conf->filter[afi][safi];
6755
6756 for (direct = FILTER_IN; direct < FILTER_MAX;
6757 direct++) {
6758 if (filter->aslist[direct].name)
6759 filter->aslist[direct]
6760 .aslist = as_list_lookup(
d62a17ae 6761 filter->aslist[direct]
05c7a1cc
QY
6762 .name);
6763 else
6764 filter->aslist[direct].aslist =
6765 NULL;
d62a17ae 6766 }
05c7a1cc 6767 }
d62a17ae 6768 }
6769 }
6770}
6771
6772static void peer_aslist_add(char *aslist_name)
6773{
6774 peer_aslist_update(aslist_name);
c4efd0f4 6775 route_map_notify_dependencies(aslist_name, RMAP_EVENT_ASLIST_ADDED);
d62a17ae 6776}
6777
6778static void peer_aslist_del(const char *aslist_name)
6779{
6780 peer_aslist_update(aslist_name);
6781 route_map_notify_dependencies(aslist_name, RMAP_EVENT_ASLIST_DELETED);
6782}
6783
6784
6785int peer_route_map_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
1de27621 6786 const char *name, struct route_map *route_map)
d62a17ae 6787{
70ee29b4 6788 struct peer *member;
d62a17ae 6789 struct bgp_filter *filter;
d62a17ae 6790 struct listnode *node, *nnode;
6791
6792 if (direct != RMAP_IN && direct != RMAP_OUT)
6793 return BGP_ERR_INVALID_VALUE;
6794
70ee29b4 6795 /* Set configuration on peer. */
d62a17ae 6796 filter = &peer->filter[afi][safi];
6c3247bd
NT
6797 if (filter->map[direct].name) {
6798 /* If the neighbor is configured with the same route-map
6799 * again then, ignore the duplicate configuration.
6800 */
6801 if (strcmp(filter->map[direct].name, name) == 0)
6802 return 0;
6803
d62a17ae 6804 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
6c3247bd 6805 }
b4897fa5 6806 route_map_counter_decrement(filter->map[direct].map);
d62a17ae 6807 filter->map[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
1de27621 6808 filter->map[direct].map = route_map;
b4897fa5 6809 route_map_counter_increment(route_map);
d62a17ae 6810
70ee29b4 6811 /* Check if handling a regular peer. */
d62a17ae 6812 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4
PM
6813 /* Set override-flag and process peer route updates. */
6814 SET_FLAG(peer->filter_override[afi][safi][direct],
6815 PEER_FT_ROUTE_MAP);
d62a17ae 6816 peer_on_policy_change(peer, afi, safi,
6817 (direct == RMAP_OUT) ? 1 : 0);
70ee29b4
PM
6818
6819 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6820 return 0;
6821 }
6822
70ee29b4
PM
6823 /*
6824 * Set configuration on all peer-group members, unless they are
f75b9163 6825 * explicitly overriding peer-group configuration.
70ee29b4
PM
6826 */
6827 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6828 /* Skip peers with overridden configuration. */
6829 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6830 PEER_FT_ROUTE_MAP))
6831 continue;
d62a17ae 6832
70ee29b4
PM
6833 /* Set configuration on peer-group member. */
6834 filter = &member->filter[afi][safi];
d62a17ae 6835 if (filter->map[direct].name)
6836 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
b4897fa5 6837 route_map_counter_decrement(filter->map[direct].map);
d62a17ae 6838 filter->map[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
1de27621 6839 filter->map[direct].map = route_map;
b4897fa5 6840 route_map_counter_increment(route_map);
70ee29b4
PM
6841
6842 /* Process peer route updates. */
6843 peer_on_policy_change(member, afi, safi,
d62a17ae 6844 (direct == RMAP_OUT) ? 1 : 0);
6845 }
6846 return 0;
6847}
6848
6849/* Unset route-map from the peer. */
6850int peer_route_map_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6851{
70ee29b4 6852 struct peer *member;
d62a17ae 6853 struct bgp_filter *filter;
d62a17ae 6854 struct listnode *node, *nnode;
6855
6856 if (direct != RMAP_IN && direct != RMAP_OUT)
6857 return BGP_ERR_INVALID_VALUE;
6858
70ee29b4
PM
6859 /* Unset override-flag unconditionally. */
6860 UNSET_FLAG(peer->filter_override[afi][safi][direct], PEER_FT_ROUTE_MAP);
d62a17ae 6861
70ee29b4 6862 /* Inherit configuration from peer-group if peer is member. */
d62a17ae 6863 if (peer_group_active(peer)) {
e7103a96
PM
6864 PEER_STR_ATTR_INHERIT(peer, peer->group,
6865 filter[afi][safi].map[direct].name,
6866 MTYPE_BGP_FILTER_NAME);
6867 PEER_ATTR_INHERIT(peer, peer->group,
6868 filter[afi][safi].map[direct].map);
70ee29b4
PM
6869 } else {
6870 /* Otherwise remove configuration from peer. */
6871 filter = &peer->filter[afi][safi];
6872 if (filter->map[direct].name)
6873 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
b4897fa5 6874 route_map_counter_decrement(filter->map[direct].map);
70ee29b4
PM
6875 filter->map[direct].name = NULL;
6876 filter->map[direct].map = NULL;
d62a17ae 6877 }
6878
70ee29b4 6879 /* Check if handling a regular peer. */
d62a17ae 6880 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4 6881 /* Process peer route updates. */
d62a17ae 6882 peer_on_policy_change(peer, afi, safi,
6883 (direct == RMAP_OUT) ? 1 : 0);
70ee29b4
PM
6884
6885 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6886 return 0;
6887 }
6888
70ee29b4
PM
6889 /*
6890 * Remove configuration on all peer-group members, unless they are
f75b9163 6891 * explicitly overriding peer-group configuration.
70ee29b4
PM
6892 */
6893 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6894 /* Skip peers with overridden configuration. */
6895 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6896 PEER_FT_ROUTE_MAP))
6897 continue;
d62a17ae 6898
70ee29b4
PM
6899 /* Remove configuration on peer-group member. */
6900 filter = &member->filter[afi][safi];
d62a17ae 6901 if (filter->map[direct].name)
6902 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
b4897fa5 6903 route_map_counter_decrement(filter->map[direct].map);
d62a17ae 6904 filter->map[direct].name = NULL;
6905 filter->map[direct].map = NULL;
70ee29b4
PM
6906
6907 /* Process peer route updates. */
6908 peer_on_policy_change(member, afi, safi,
d62a17ae 6909 (direct == RMAP_OUT) ? 1 : 0);
6910 }
70ee29b4 6911
d62a17ae 6912 return 0;
6913}
6914
6915/* Set unsuppress-map to the peer. */
6916int peer_unsuppress_map_set(struct peer *peer, afi_t afi, safi_t safi,
1de27621 6917 const char *name, struct route_map *route_map)
d62a17ae 6918{
70ee29b4 6919 struct peer *member;
d62a17ae 6920 struct bgp_filter *filter;
d62a17ae 6921 struct listnode *node, *nnode;
6922
70ee29b4 6923 /* Set configuration on peer. */
d62a17ae 6924 filter = &peer->filter[afi][safi];
d62a17ae 6925 if (filter->usmap.name)
6926 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
b4897fa5 6927 route_map_counter_decrement(filter->usmap.map);
d62a17ae 6928 filter->usmap.name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
1de27621 6929 filter->usmap.map = route_map;
b4897fa5 6930 route_map_counter_increment(route_map);
d62a17ae 6931
70ee29b4 6932 /* Check if handling a regular peer. */
d62a17ae 6933 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4
PM
6934 /* Set override-flag and process peer route updates. */
6935 SET_FLAG(peer->filter_override[afi][safi][0],
6936 PEER_FT_UNSUPPRESS_MAP);
d62a17ae 6937 peer_on_policy_change(peer, afi, safi, 1);
70ee29b4
PM
6938
6939 /* Skip peer-group mechanics for regular peers. */
d62a17ae 6940 return 0;
6941 }
6942
70ee29b4
PM
6943 /*
6944 * Set configuration on all peer-group members, unless they are
f75b9163 6945 * explicitly overriding peer-group configuration.
70ee29b4
PM
6946 */
6947 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6948 /* Skip peers with overridden configuration. */
246bb5f0 6949 if (CHECK_FLAG(member->filter_override[afi][safi][0],
70ee29b4
PM
6950 PEER_FT_UNSUPPRESS_MAP))
6951 continue;
d62a17ae 6952
70ee29b4
PM
6953 /* Set configuration on peer-group member. */
6954 filter = &member->filter[afi][safi];
d62a17ae 6955 if (filter->usmap.name)
6956 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
b4897fa5 6957 route_map_counter_decrement(filter->usmap.map);
d62a17ae 6958 filter->usmap.name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
1de27621 6959 filter->usmap.map = route_map;
b4897fa5 6960 route_map_counter_increment(route_map);
70ee29b4
PM
6961
6962 /* Process peer route updates. */
6963 peer_on_policy_change(member, afi, safi, 1);
d62a17ae 6964 }
70ee29b4 6965
d62a17ae 6966 return 0;
6967}
6968
6969/* Unset route-map from the peer. */
6970int peer_unsuppress_map_unset(struct peer *peer, afi_t afi, safi_t safi)
718e3744 6971{
70ee29b4 6972 struct peer *member;
d62a17ae 6973 struct bgp_filter *filter;
d62a17ae 6974 struct listnode *node, *nnode;
6975
70ee29b4
PM
6976 /* Unset override-flag unconditionally. */
6977 UNSET_FLAG(peer->filter_override[afi][safi][0], PEER_FT_UNSUPPRESS_MAP);
718e3744 6978
70ee29b4
PM
6979 /* Inherit configuration from peer-group if peer is member. */
6980 if (peer_group_active(peer)) {
e7103a96
PM
6981 PEER_STR_ATTR_INHERIT(peer, peer->group,
6982 filter[afi][safi].usmap.name,
6983 MTYPE_BGP_FILTER_NAME);
6984 PEER_ATTR_INHERIT(peer, peer->group,
6985 filter[afi][safi].usmap.map);
70ee29b4
PM
6986 } else {
6987 /* Otherwise remove configuration from peer. */
6988 filter = &peer->filter[afi][safi];
6989 if (filter->usmap.name)
6990 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
b4897fa5 6991 route_map_counter_decrement(filter->usmap.map);
70ee29b4
PM
6992 filter->usmap.name = NULL;
6993 filter->usmap.map = NULL;
6994 }
718e3744 6995
70ee29b4 6996 /* Check if handling a regular peer. */
d62a17ae 6997 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
70ee29b4 6998 /* Process peer route updates. */
d62a17ae 6999 peer_on_policy_change(peer, afi, safi, 1);
70ee29b4
PM
7000
7001 /* Skip peer-group mechanics for regular peers. */
d62a17ae 7002 return 0;
7003 }
718e3744 7004
70ee29b4
PM
7005 /*
7006 * Remove configuration on all peer-group members, unless they are
f75b9163 7007 * explicitly overriding peer-group configuration.
70ee29b4
PM
7008 */
7009 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7010 /* Skip peers with overridden configuration. */
7011 if (CHECK_FLAG(member->filter_override[afi][safi][0],
7012 PEER_FT_UNSUPPRESS_MAP))
7013 continue;
718e3744 7014
70ee29b4
PM
7015 /* Remove configuration on peer-group member. */
7016 filter = &member->filter[afi][safi];
d62a17ae 7017 if (filter->usmap.name)
7018 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
b4897fa5 7019 route_map_counter_decrement(filter->usmap.map);
d62a17ae 7020 filter->usmap.name = NULL;
7021 filter->usmap.map = NULL;
70ee29b4
PM
7022
7023 /* Process peer route updates. */
7024 peer_on_policy_change(member, afi, safi, 1);
d62a17ae 7025 }
70ee29b4 7026
d62a17ae 7027 return 0;
7028}
718e3744 7029
fa36596c
MK
7030static void peer_advertise_map_filter_update(struct peer *peer, afi_t afi,
7031 safi_t safi, const char *amap_name,
7032 struct route_map *amap,
7033 const char *cmap_name,
7034 struct route_map *cmap,
7035 bool condition, bool set)
cf2ad4d8
MK
7036{
7037 struct bgp_filter *filter;
7038 bool filter_exists = false;
7039
7040 filter = &peer->filter[afi][safi];
7041
fa36596c
MK
7042 /* advertise-map is already configured. */
7043 if (filter->advmap.aname) {
7044 filter_exists = true;
7045 XFREE(MTYPE_BGP_FILTER_NAME, filter->advmap.aname);
7046 XFREE(MTYPE_BGP_FILTER_NAME, filter->advmap.cname);
7047 }
cf2ad4d8 7048
fa36596c 7049 route_map_counter_decrement(filter->advmap.amap);
cf2ad4d8 7050
fa36596c
MK
7051 /* Removed advertise-map configuration */
7052 if (!set) {
5b083e4e 7053 memset(&filter->advmap, 0, sizeof(filter->advmap));
cf2ad4d8 7054
fa36596c
MK
7055 /* decrement condition_filter_count delete timer if
7056 * this is the last advertise-map to be removed.
7057 */
7058 if (filter_exists)
7059 bgp_conditional_adv_disable(peer, afi, safi);
cf2ad4d8 7060
fa36596c
MK
7061 return;
7062 }
7063
7064 /* Update filter data with newly configured values. */
7065 filter->advmap.aname = XSTRDUP(MTYPE_BGP_FILTER_NAME, amap_name);
7066 filter->advmap.cname = XSTRDUP(MTYPE_BGP_FILTER_NAME, cmap_name);
7067 filter->advmap.amap = amap;
7068 filter->advmap.cmap = cmap;
7069 filter->advmap.condition = condition;
7070 route_map_counter_increment(filter->advmap.amap);
7071 peer->advmap_config_change[afi][safi] = true;
7072
7073 /* Increment condition_filter_count and/or create timer. */
7074 if (!filter_exists) {
7075 filter->advmap.update_type = ADVERTISE;
7076 bgp_conditional_adv_enable(peer, afi, safi);
cf2ad4d8
MK
7077 }
7078}
7079
7f7940e6
MK
7080/* Set advertise-map to the peer but do not process peer route updates here. *
7081 * Hold filter changes until the conditional routes polling thread is called *
7082 * AS we need to advertise/withdraw prefixes (in advertise-map) based on the *
7083 * condition (exist-map/non-exist-map) and routes(specified in condition-map) *
7084 * in BGP table. So do not call peer_on_policy_change() here, only create *
7085 * polling timer thread, update filters and increment condition_filter_count.
7086 */
7087int peer_advertise_map_set(struct peer *peer, afi_t afi, safi_t safi,
7088 const char *advertise_name,
cf2ad4d8 7089 struct route_map *advertise_map,
7f7940e6 7090 const char *condition_name,
cf2ad4d8 7091 struct route_map *condition_map, bool condition)
7f7940e6 7092{
7f7940e6 7093 struct peer *member;
7f7940e6
MK
7094 struct listnode *node, *nnode;
7095
cf2ad4d8 7096 /* Set configuration on peer. */
fa36596c
MK
7097 peer_advertise_map_filter_update(peer, afi, safi, advertise_name,
7098 advertise_map, condition_name,
7099 condition_map, condition, true);
7f7940e6 7100
cf2ad4d8 7101 /* Check if handling a regular peer & Skip peer-group mechanics. */
7f7940e6
MK
7102 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7103 /* Set override-flag and process peer route updates. */
7104 SET_FLAG(peer->filter_override[afi][safi][RMAP_OUT],
7105 PEER_FT_ADVERTISE_MAP);
7f7940e6
MK
7106 return 0;
7107 }
7108
7109 /*
7110 * Set configuration on all peer-group members, unless they are
f75b9163 7111 * explicitly overriding peer-group configuration.
7f7940e6
MK
7112 */
7113 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7114 /* Skip peers with overridden configuration. */
7115 if (CHECK_FLAG(member->filter_override[afi][safi][RMAP_OUT],
7116 PEER_FT_ADVERTISE_MAP))
7117 continue;
7118
7119 /* Set configuration on peer-group member. */
fa36596c
MK
7120 peer_advertise_map_filter_update(
7121 member, afi, safi, advertise_name, advertise_map,
7122 condition_name, condition_map, condition, true);
7f7940e6
MK
7123 }
7124
7125 return 0;
7126}
7127
7128/* Unset advertise-map from the peer. */
7129int peer_advertise_map_unset(struct peer *peer, afi_t afi, safi_t safi,
7130 const char *advertise_name,
cf2ad4d8 7131 struct route_map *advertise_map,
7f7940e6 7132 const char *condition_name,
cf2ad4d8 7133 struct route_map *condition_map, bool condition)
7f7940e6 7134{
7f7940e6 7135 struct peer *member;
7f7940e6
MK
7136 struct listnode *node, *nnode;
7137
fa36596c
MK
7138 /* advertise-map is not configured */
7139 if (!peer->filter[afi][safi].advmap.aname)
7140 return 0;
cf2ad4d8 7141
7f7940e6
MK
7142 /* Unset override-flag unconditionally. */
7143 UNSET_FLAG(peer->filter_override[afi][safi][RMAP_OUT],
7144 PEER_FT_ADVERTISE_MAP);
7145
7146 /* Inherit configuration from peer-group if peer is member. */
7147 if (peer_group_active(peer)) {
7148 PEER_STR_ATTR_INHERIT(peer, peer->group,
7149 filter[afi][safi].advmap.aname,
7150 MTYPE_BGP_FILTER_NAME);
7151 PEER_ATTR_INHERIT(peer, peer->group,
7152 filter[afi][safi].advmap.amap);
cf2ad4d8 7153 } else
fa36596c
MK
7154 peer_advertise_map_filter_update(
7155 peer, afi, safi, advertise_name, advertise_map,
7156 condition_name, condition_map, condition, false);
7f7940e6 7157
cf2ad4d8 7158 /* Check if handling a regular peer and skip peer-group mechanics. */
7f7940e6
MK
7159 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7160 /* Process peer route updates. */
e73c112e
MK
7161 if (BGP_DEBUG(update, UPDATE_OUT))
7162 zlog_debug("%s: Send normal update to %s for %s",
7163 __func__, peer->host,
7164 get_afi_safi_str(afi, safi, false));
7165
7f7940e6 7166 peer_on_policy_change(peer, afi, safi, 1);
7f7940e6
MK
7167 return 0;
7168 }
7169
7170 /*
7171 * Remove configuration on all peer-group members, unless they are
f75b9163 7172 * explicitly overriding peer-group configuration.
7f7940e6
MK
7173 */
7174 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7175 /* Skip peers with overridden configuration. */
7176 if (CHECK_FLAG(member->filter_override[afi][safi][RMAP_OUT],
7177 PEER_FT_ADVERTISE_MAP))
7178 continue;
7f7940e6 7179 /* Remove configuration on peer-group member. */
fa36596c
MK
7180 peer_advertise_map_filter_update(
7181 member, afi, safi, advertise_name, advertise_map,
7182 condition_name, condition_map, condition, false);
7f7940e6
MK
7183
7184 /* Process peer route updates. */
e73c112e
MK
7185 if (BGP_DEBUG(update, UPDATE_OUT))
7186 zlog_debug("%s: Send normal update to %s for %s ",
7187 __func__, member->host,
7188 get_afi_safi_str(afi, safi, false));
7189
7f7940e6
MK
7190 peer_on_policy_change(member, afi, safi, 1);
7191 }
7192
7193 return 0;
7194}
7195
ac4522e6
DM
7196static bool peer_maximum_prefix_clear_overflow(struct peer *peer)
7197{
7198 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
7199 return false;
7200
7201 UNSET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
7202 if (peer->t_pmax_restart) {
7203 BGP_TIMER_OFF(peer->t_pmax_restart);
7204 if (bgp_debug_neighbor_events(peer))
a7d91a8c 7205 zlog_debug(
f70c91dc
DA
7206 "%pBP Maximum-prefix restart timer cancelled",
7207 peer);
ac4522e6
DM
7208 }
7209 BGP_EVENT_ADD(peer, BGP_Start);
7210 return true;
7211}
7212
d62a17ae 7213int peer_maximum_prefix_set(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a 7214 uint32_t max, uint8_t threshold, int warning,
9cbd06e0 7215 uint16_t restart, bool force)
d62a17ae 7216{
598ce6bd 7217 struct peer *member;
d62a17ae 7218 struct listnode *node, *nnode;
7219
598ce6bd
PM
7220 /* Set flags and configuration on peer. */
7221 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
9cbd06e0
DA
7222
7223 if (force)
7224 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7225 else
7226 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7227
d62a17ae 7228 if (warning)
598ce6bd 7229 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_WARNING);
d62a17ae 7230 else
598ce6bd
PM
7231 peer_af_flag_unset(peer, afi, safi,
7232 PEER_FLAG_MAX_PREFIX_WARNING);
7233
04e1c5bb
QY
7234 peer->pmax[afi][safi] = max;
7235 peer->pmax_threshold[afi][safi] = threshold;
7236 peer->pmax_restart[afi][safi] = restart;
d62a17ae 7237
598ce6bd
PM
7238 /* Check if handling a regular peer. */
7239 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7240 /* Re-check if peer violates maximum-prefix. */
feb17238 7241 if ((peer_established(peer)) && (peer->afc[afi][safi]))
d62a17ae 7242 bgp_maximum_prefix_overflow(peer, afi, safi, 1);
04e1c5bb 7243
598ce6bd
PM
7244 /* Skip peer-group mechanics for regular peers. */
7245 return 0;
7246 }
04e1c5bb 7247
598ce6bd
PM
7248 /*
7249 * Set flags and configuration on all peer-group members, unless they
f75b9163 7250 * are explicitly overriding peer-group configuration.
598ce6bd
PM
7251 */
7252 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7253 /* Skip peers with overridden configuration. */
7254 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7255 PEER_FLAG_MAX_PREFIX))
7256 continue;
04e1c5bb 7257
598ce6bd
PM
7258 /* Set flag and configuration on peer-group member. */
7259 member->pmax[afi][safi] = max;
7260 member->pmax_threshold[afi][safi] = threshold;
7261 member->pmax_restart[afi][safi] = restart;
9cbd06e0
DA
7262
7263 if (force)
7264 SET_FLAG(member->af_flags[afi][safi],
7265 PEER_FLAG_MAX_PREFIX_FORCE);
7266 else
7267 UNSET_FLAG(member->af_flags[afi][safi],
7268 PEER_FLAG_MAX_PREFIX_FORCE);
7269
04e1c5bb 7270 if (warning)
598ce6bd 7271 SET_FLAG(member->af_flags[afi][safi],
04e1c5bb
QY
7272 PEER_FLAG_MAX_PREFIX_WARNING);
7273 else
598ce6bd 7274 UNSET_FLAG(member->af_flags[afi][safi],
04e1c5bb 7275 PEER_FLAG_MAX_PREFIX_WARNING);
598ce6bd
PM
7276
7277 /* Re-check if peer violates maximum-prefix. */
feb17238 7278 if ((peer_established(member)) && (member->afc[afi][safi]))
598ce6bd 7279 bgp_maximum_prefix_overflow(member, afi, safi, 1);
d62a17ae 7280 }
718e3744 7281
d62a17ae 7282 return 0;
7283}
718e3744 7284
d62a17ae 7285int peer_maximum_prefix_unset(struct peer *peer, afi_t afi, safi_t safi)
7286{
598ce6bd
PM
7287 /* Inherit configuration from peer-group if peer is member. */
7288 if (peer_group_active(peer)) {
7289 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
9cbd06e0
DA
7290 peer_af_flag_inherit(peer, afi, safi,
7291 PEER_FLAG_MAX_PREFIX_FORCE);
598ce6bd
PM
7292 peer_af_flag_inherit(peer, afi, safi,
7293 PEER_FLAG_MAX_PREFIX_WARNING);
e7103a96
PM
7294 PEER_ATTR_INHERIT(peer, peer->group, pmax[afi][safi]);
7295 PEER_ATTR_INHERIT(peer, peer->group, pmax_threshold[afi][safi]);
7296 PEER_ATTR_INHERIT(peer, peer->group, pmax_restart[afi][safi]);
04e1c5bb 7297
d62a17ae 7298 return 0;
04e1c5bb 7299 }
d62a17ae 7300
598ce6bd
PM
7301 /* Remove flags and configuration from peer. */
7302 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
9cbd06e0 7303 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
598ce6bd
PM
7304 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_WARNING);
7305 peer->pmax[afi][safi] = 0;
7306 peer->pmax_threshold[afi][safi] = 0;
7307 peer->pmax_restart[afi][safi] = 0;
7308
04e1c5bb 7309 /*
598ce6bd 7310 * Remove flags and configuration from all peer-group members, unless
f75b9163 7311 * they are explicitly overriding peer-group configuration.
04e1c5bb 7312 */
6282637d
RW
7313 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7314 struct peer *member;
7315 struct listnode *node;
598ce6bd 7316
6282637d
RW
7317 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
7318 /* Skip peers with overridden configuration. */
7319 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7320 PEER_FLAG_MAX_PREFIX))
7321 continue;
7322
7323 /* Remove flag and configuration on peer-group member.
7324 */
7325 UNSET_FLAG(member->af_flags[afi][safi],
7326 PEER_FLAG_MAX_PREFIX);
9cbd06e0
DA
7327 UNSET_FLAG(member->af_flags[afi][safi],
7328 PEER_FLAG_MAX_PREFIX_FORCE);
6282637d
RW
7329 UNSET_FLAG(member->af_flags[afi][safi],
7330 PEER_FLAG_MAX_PREFIX_WARNING);
7331 member->pmax[afi][safi] = 0;
7332 member->pmax_threshold[afi][safi] = 0;
7333 member->pmax_restart[afi][safi] = 0;
ac4522e6
DM
7334
7335 peer_maximum_prefix_clear_overflow(member);
6282637d 7336 }
ac4522e6
DM
7337 } else {
7338 peer_maximum_prefix_clear_overflow(peer);
d62a17ae 7339 }
598ce6bd 7340
d62a17ae 7341 return 0;
718e3744 7342}
7343
d0bf49ec
LS
7344void peer_maximum_prefix_out_refresh_routes(struct peer *peer, afi_t afi,
7345 safi_t safi)
7346{
7347 update_group_adjust_peer(peer_af_find(peer, afi, safi));
7348
7349 if (peer_established(peer))
7350 bgp_announce_route(peer, afi, safi, false);
7351}
7352
80444d30
LS
7353int peer_maximum_prefix_out_set(struct peer *peer, afi_t afi, safi_t safi,
7354 uint32_t max)
7355{
7356 struct peer *member;
7357 struct listnode *node, *nnode;
7358
7359 /* Set flag on peer and peer-group member if any */
7360 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7361 /* Set configuration on peer. */
7362 peer->pmax_out[afi][safi] = max;
7363
7364 /* Check if handling a regular peer. */
7365 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7366 /* Skip peer-group mechanics for regular peers. */
7367 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7368 return 0;
7369 }
7370
7371 /*
7372 * Set flag and configuration on all peer-group members, unless they
f75b9163 7373 * are explicitly overriding peer-group configuration.
80444d30
LS
7374 */
7375 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7376 /* Skip peers with overridden configuration. */
7377 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7378 PEER_FLAG_MAX_PREFIX_OUT))
7379 continue;
7380
7381 /* Set configuration on peer-group member. */
7382 member->pmax_out[afi][safi] = max;
7383
7384 peer_maximum_prefix_out_refresh_routes(member, afi, safi);
7385 }
7386 return 0;
7387}
7388
7389int peer_maximum_prefix_out_unset(struct peer *peer, afi_t afi, safi_t safi)
7390{
7391 struct peer *member;
7392 struct listnode *node;
7393 /* Inherit configuration from peer-group if peer is member. */
7394 if (peer_group_active(peer)) {
7395 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7396 PEER_ATTR_INHERIT(peer, peer->group, pmax_out[afi][safi]);
7397
7398 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7399 return 0;
7400 }
7401
7402 /* Remove flag and configuration from peer. */
7403 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7404 peer->pmax_out[afi][safi] = 0;
7405
7406 /* Check if handling a regular peer. */
7407 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7408 /* Skip peer-group mechanics for regular peers. */
7409 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7410 return 0;
7411 }
7412
7413 /*
7414 * Remove flag and configuration from all peer-group members, unless
f75b9163 7415 * they are explicitly overriding peer-group configuration.
80444d30
LS
7416 */
7417 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
7418 /* Skip peers with overridden configuration. */
7419 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7420 PEER_FLAG_MAX_PREFIX_OUT))
7421 continue;
7422
7423 /* Remove flag and configuration on peer-group member.
7424 */
7425 UNSET_FLAG(member->af_flags[afi][safi],
7426 PEER_FLAG_MAX_PREFIX_OUT);
7427 member->pmax_out[afi][safi] = 0;
7428
7429 peer_maximum_prefix_out_refresh_routes(member, afi, safi);
7430 }
7431 return 0;
7432}
7433
d62a17ae 7434int is_ebgp_multihop_configured(struct peer *peer)
718e3744 7435{
d62a17ae 7436 struct peer_group *group;
7437 struct listnode *node, *nnode;
7438 struct peer *peer1;
718e3744 7439
d62a17ae 7440 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7441 group = peer->group;
7442 if ((peer_sort(peer) != BGP_PEER_IBGP)
c8d6f0d6 7443 && (group->conf->ttl != BGP_DEFAULT_TTL))
d62a17ae 7444 return 1;
718e3744 7445
d62a17ae 7446 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer1)) {
7447 if ((peer_sort(peer1) != BGP_PEER_IBGP)
c8d6f0d6 7448 && (peer1->ttl != BGP_DEFAULT_TTL))
d62a17ae 7449 return 1;
7450 }
7451 } else {
c8d6f0d6
DA
7452 if ((peer_sort(peer) != BGP_PEER_IBGP)
7453 && (peer->ttl != BGP_DEFAULT_TTL))
d62a17ae 7454 return 1;
7455 }
7456 return 0;
7457}
718e3744 7458
d62a17ae 7459/* Set # of hops between us and BGP peer. */
7460int peer_ttl_security_hops_set(struct peer *peer, int gtsm_hops)
7461{
7462 struct peer_group *group;
4ec7a71a 7463 struct peer *gpeer;
d62a17ae 7464 struct listnode *node, *nnode;
7465 int ret;
7466
84c320dc
DA
7467 zlog_debug("%s: set gtsm_hops to %d for %s", __func__, gtsm_hops,
7468 peer->host);
d62a17ae 7469
7470 /* We cannot configure ttl-security hops when ebgp-multihop is already
7471 set. For non peer-groups, the check is simple. For peer-groups,
7472 it's
7473 slightly messy, because we need to check both the peer-group
7474 structure
7475 and all peer-group members for any trace of ebgp-multihop
7476 configuration
7477 before actually applying the ttl-security rules. Cisco really made a
7478 mess of this configuration parameter, and OpenBGPD got it right.
7479 */
7480
e2521429
DA
7481 if ((peer->gtsm_hops == BGP_GTSM_HOPS_DISABLED)
7482 && (peer->sort != BGP_PEER_IBGP)) {
d62a17ae 7483 if (is_ebgp_multihop_configured(peer))
7484 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
7485
7486 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7487 peer->gtsm_hops = gtsm_hops;
7488
7489 /* Calling ebgp multihop also resets the session.
7490 * On restart, NHT will get setup correctly as will the
7491 * min & max ttls on the socket. The return value is
7492 * irrelevant.
7493 */
7494 ret = peer_ebgp_multihop_set(peer, MAXTTL);
7495
7496 if (ret != 0)
7497 return ret;
7498 } else {
7499 group = peer->group;
4ec7a71a 7500 group->conf->gtsm_hops = gtsm_hops;
d62a17ae 7501 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
4ec7a71a
DS
7502 gpeer)) {
7503 gpeer->gtsm_hops = group->conf->gtsm_hops;
d62a17ae 7504
7505 /* Calling ebgp multihop also resets the
7506 * session.
7507 * On restart, NHT will get setup correctly as
7508 * will the
7509 * min & max ttls on the socket. The return
7510 * value is
7511 * irrelevant.
7512 */
4ec7a71a 7513 peer_ebgp_multihop_set(gpeer, MAXTTL);
d62a17ae 7514 }
7515 }
7516 } else {
7517 /* Post the first gtsm setup or if its ibgp, maxttl setting
7518 * isn't
7519 * necessary, just set the minttl.
7520 */
7521 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7522 peer->gtsm_hops = gtsm_hops;
7523
7524 if (peer->fd >= 0)
7525 sockopt_minttl(peer->su.sa.sa_family, peer->fd,
7526 MAXTTL + 1 - gtsm_hops);
7527 if ((peer->status < Established) && peer->doppelganger
7528 && (peer->doppelganger->fd >= 0))
7529 sockopt_minttl(peer->su.sa.sa_family,
7530 peer->doppelganger->fd,
7531 MAXTTL + 1 - gtsm_hops);
7532 } else {
7533 group = peer->group;
4ec7a71a 7534 group->conf->gtsm_hops = gtsm_hops;
d62a17ae 7535 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
4ec7a71a
DS
7536 gpeer)) {
7537 gpeer->gtsm_hops = group->conf->gtsm_hops;
d62a17ae 7538
7539 /* Change setting of existing peer
7540 * established then change value (may break
7541 * connectivity)
7542 * not established yet (teardown session and
7543 * restart)
7544 * no session then do nothing (will get
7545 * handled by next connection)
7546 */
4ec7a71a
DS
7547 if (gpeer->fd >= 0
7548 && gpeer->gtsm_hops
e2521429 7549 != BGP_GTSM_HOPS_DISABLED)
d62a17ae 7550 sockopt_minttl(
4ec7a71a
DS
7551 gpeer->su.sa.sa_family,
7552 gpeer->fd,
7553 MAXTTL + 1 - gpeer->gtsm_hops);
7554 if ((gpeer->status < Established)
7555 && gpeer->doppelganger
7556 && (gpeer->doppelganger->fd >= 0))
7557 sockopt_minttl(gpeer->su.sa.sa_family,
7558 gpeer->doppelganger->fd,
d62a17ae 7559 MAXTTL + 1 - gtsm_hops);
7560 }
7561 }
7562 }
718e3744 7563
d62a17ae 7564 return 0;
718e3744 7565}
7566
d62a17ae 7567int peer_ttl_security_hops_unset(struct peer *peer)
7568{
7569 struct peer_group *group;
7570 struct listnode *node, *nnode;
7571 int ret = 0;
7572
84c320dc 7573 zlog_debug("%s: set gtsm_hops to zero for %s", __func__, peer->host);
d62a17ae 7574
7575 /* if a peer-group member, then reset to peer-group default rather than
7576 * 0 */
7577 if (peer_group_active(peer))
7578 peer->gtsm_hops = peer->group->conf->gtsm_hops;
7579 else
e2521429 7580 peer->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
d62a17ae 7581
7582 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7583 /* Invoking ebgp_multihop_set will set the TTL back to the
7584 * original
7585 * value as well as restting the NHT and such. The session is
7586 * reset.
7587 */
7588 if (peer->sort == BGP_PEER_EBGP)
7589 ret = peer_ebgp_multihop_unset(peer);
7590 else {
7591 if (peer->fd >= 0)
7592 sockopt_minttl(peer->su.sa.sa_family, peer->fd,
7593 0);
7594
7595 if ((peer->status < Established) && peer->doppelganger
7596 && (peer->doppelganger->fd >= 0))
7597 sockopt_minttl(peer->su.sa.sa_family,
7598 peer->doppelganger->fd, 0);
7599 }
7600 } else {
7601 group = peer->group;
7602 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
e2521429 7603 peer->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
d62a17ae 7604 if (peer->sort == BGP_PEER_EBGP)
7605 ret = peer_ebgp_multihop_unset(peer);
7606 else {
7607 if (peer->fd >= 0)
7608 sockopt_minttl(peer->su.sa.sa_family,
7609 peer->fd, 0);
7610
7611 if ((peer->status < Established)
7612 && peer->doppelganger
7613 && (peer->doppelganger->fd >= 0))
7614 sockopt_minttl(peer->su.sa.sa_family,
7615 peer->doppelganger->fd,
7616 0);
7617 }
7618 }
7619 }
718e3744 7620
d62a17ae 7621 return ret;
7622}
7623
3cb14f26
DS
7624static void peer_reset_message_stats(struct peer *peer)
7625{
7626 if (peer) {
7627 atomic_store_explicit(&peer->open_in, 0, memory_order_relaxed);
7628 atomic_store_explicit(&peer->open_out, 0, memory_order_relaxed);
7629 atomic_store_explicit(&peer->update_in, 0,
7630 memory_order_relaxed);
7631 atomic_store_explicit(&peer->update_out, 0,
7632 memory_order_relaxed);
7633 atomic_store_explicit(&peer->keepalive_in, 0,
7634 memory_order_relaxed);
7635 atomic_store_explicit(&peer->keepalive_out, 0,
7636 memory_order_relaxed);
7637 atomic_store_explicit(&peer->notify_in, 0,
7638 memory_order_relaxed);
7639 atomic_store_explicit(&peer->notify_out, 0,
7640 memory_order_relaxed);
7641 atomic_store_explicit(&peer->refresh_in, 0,
7642 memory_order_relaxed);
7643 atomic_store_explicit(&peer->refresh_out, 0,
7644 memory_order_relaxed);
7645 atomic_store_explicit(&peer->dynamic_cap_in, 0,
7646 memory_order_relaxed);
7647 atomic_store_explicit(&peer->dynamic_cap_out, 0,
7648 memory_order_relaxed);
7649 }
7650}
7651
d62a17ae 7652/*
7653 * If peer clear is invoked in a loop for all peers on the BGP instance,
7654 * it may end up freeing the doppelganger, and if this was the next node
7655 * to the current node, we would end up accessing the freed next node.
7656 * Pass along additional parameter which can be updated if next node
7657 * is freed; only required when walking the peer list on BGP instance.
7658 */
7659int peer_clear(struct peer *peer, struct listnode **nnode)
7660{
cb9196e7
DS
7661 if (!CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN)
7662 || !CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHUTDOWN)) {
ac4522e6 7663 if (peer_maximum_prefix_clear_overflow(peer))
d62a17ae 7664 return 0;
d62a17ae 7665
7666 peer->v_start = BGP_INIT_START_TIMER;
7667 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
7668 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
7669 BGP_NOTIFY_CEASE_ADMIN_RESET);
7670 else
7671 bgp_session_reset_safe(peer, nnode);
718e3744 7672 }
d62a17ae 7673 return 0;
718e3744 7674}
6b0655a2 7675
d62a17ae 7676int peer_clear_soft(struct peer *peer, afi_t afi, safi_t safi,
7677 enum bgp_clear_type stype)
718e3744 7678{
d62a17ae 7679 struct peer_af *paf;
718e3744 7680
feb17238 7681 if (!peer_established(peer))
d62a17ae 7682 return 0;
718e3744 7683
d62a17ae 7684 if (!peer->afc[afi][safi])
7685 return BGP_ERR_AF_UNCONFIGURED;
718e3744 7686
d62a17ae 7687 peer->rtt = sockopt_tcp_rtt(peer->fd);
718e3744 7688
d62a17ae 7689 if (stype == BGP_CLEAR_SOFT_OUT || stype == BGP_CLEAR_SOFT_BOTH) {
7690 /* Clear the "neighbor x.x.x.x default-originate" flag */
7691 paf = peer_af_find(peer, afi, safi);
7692 if (paf && paf->subgroup
7693 && CHECK_FLAG(paf->subgroup->sflags,
7694 SUBGRP_STATUS_DEFAULT_ORIGINATE))
7695 UNSET_FLAG(paf->subgroup->sflags,
7696 SUBGRP_STATUS_DEFAULT_ORIGINATE);
718e3744 7697
e1a32ec1 7698 bgp_announce_route(peer, afi, safi, false);
d62a17ae 7699 }
718e3744 7700
d62a17ae 7701 if (stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) {
7702 if (CHECK_FLAG(peer->af_cap[afi][safi],
7703 PEER_CAP_ORF_PREFIX_SM_ADV)
7704 && (CHECK_FLAG(peer->af_cap[afi][safi],
7705 PEER_CAP_ORF_PREFIX_RM_RCV)
7706 || CHECK_FLAG(peer->af_cap[afi][safi],
7707 PEER_CAP_ORF_PREFIX_RM_OLD_RCV))) {
7708 struct bgp_filter *filter = &peer->filter[afi][safi];
d7c0a89a 7709 uint8_t prefix_type;
d62a17ae 7710
7711 if (CHECK_FLAG(peer->af_cap[afi][safi],
7712 PEER_CAP_ORF_PREFIX_RM_RCV))
7713 prefix_type = ORF_TYPE_PREFIX;
7714 else
7715 prefix_type = ORF_TYPE_PREFIX_OLD;
7716
7717 if (filter->plist[FILTER_IN].plist) {
7718 if (CHECK_FLAG(peer->af_sflags[afi][safi],
7719 PEER_STATUS_ORF_PREFIX_SEND))
7720 bgp_route_refresh_send(
7721 peer, afi, safi, prefix_type,
9af52ccf
DA
7722 REFRESH_DEFER, 1,
7723 BGP_ROUTE_REFRESH_NORMAL);
7724 bgp_route_refresh_send(
7725 peer, afi, safi, prefix_type,
7726 REFRESH_IMMEDIATE, 0,
7727 BGP_ROUTE_REFRESH_NORMAL);
d62a17ae 7728 } else {
7729 if (CHECK_FLAG(peer->af_sflags[afi][safi],
7730 PEER_STATUS_ORF_PREFIX_SEND))
7731 bgp_route_refresh_send(
7732 peer, afi, safi, prefix_type,
9af52ccf
DA
7733 REFRESH_IMMEDIATE, 1,
7734 BGP_ROUTE_REFRESH_NORMAL);
d62a17ae 7735 else
9af52ccf
DA
7736 bgp_route_refresh_send(
7737 peer, afi, safi, 0, 0, 0,
7738 BGP_ROUTE_REFRESH_NORMAL);
d62a17ae 7739 }
7740 return 0;
7741 }
7742 }
718e3744 7743
d62a17ae 7744 if (stype == BGP_CLEAR_SOFT_IN || stype == BGP_CLEAR_SOFT_BOTH
7745 || stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) {
7746 /* If neighbor has soft reconfiguration inbound flag.
7747 Use Adj-RIB-In database. */
7748 if (CHECK_FLAG(peer->af_flags[afi][safi],
7749 PEER_FLAG_SOFT_RECONFIG))
7750 bgp_soft_reconfig_in(peer, afi, safi);
7751 else {
7752 /* If neighbor has route refresh capability, send route
7753 refresh
7754 message to the peer. */
7755 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
7756 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
9af52ccf
DA
7757 bgp_route_refresh_send(
7758 peer, afi, safi, 0, 0, 0,
7759 BGP_ROUTE_REFRESH_NORMAL);
d62a17ae 7760 else
7761 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED;
7762 }
7763 }
3cb14f26
DS
7764
7765 if (stype == BGP_CLEAR_MESSAGE_STATS)
7766 peer_reset_message_stats(peer);
7767
d62a17ae 7768 return 0;
718e3744 7769}
7770
d62a17ae 7771/* Display peer uptime.*/
9f049418 7772char *peer_uptime(time_t uptime2, char *buf, size_t len, bool use_json,
d62a17ae 7773 json_object *json)
7774{
7775 time_t uptime1, epoch_tbuf;
a2700b50 7776 struct tm tm;
d62a17ae 7777
d62a17ae 7778 /* If there is no connection has been done before print `never'. */
7779 if (uptime2 == 0) {
7780 if (use_json) {
7781 json_object_string_add(json, "peerUptime", "never");
7782 json_object_int_add(json, "peerUptimeMsec", 0);
7783 } else
7784 snprintf(buf, len, "never");
7785 return buf;
7786 }
718e3744 7787
d62a17ae 7788 /* Get current time. */
7789 uptime1 = bgp_clock();
7790 uptime1 -= uptime2;
a2700b50 7791 gmtime_r(&uptime1, &tm);
718e3744 7792
d62a17ae 7793 if (uptime1 < ONE_DAY_SECOND)
a2700b50
MS
7794 snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
7795 tm.tm_sec);
d62a17ae 7796 else if (uptime1 < ONE_WEEK_SECOND)
a2700b50
MS
7797 snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
7798 tm.tm_min);
d62a17ae 7799 else if (uptime1 < ONE_YEAR_SECOND)
a2700b50
MS
7800 snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7,
7801 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
d62a17ae 7802 else
a2700b50
MS
7803 snprintf(buf, len, "%02dy%02dw%dd", tm.tm_year - 70,
7804 tm.tm_yday / 7,
7805 tm.tm_yday - ((tm.tm_yday / 7) * 7));
d62a17ae 7806
7807 if (use_json) {
7808 epoch_tbuf = time(NULL) - uptime1;
7809 json_object_string_add(json, "peerUptime", buf);
7810 json_object_int_add(json, "peerUptimeMsec", uptime1 * 1000);
7811 json_object_int_add(json, "peerUptimeEstablishedEpoch",
7812 epoch_tbuf);
7813 }
718e3744 7814
d62a17ae 7815 return buf;
718e3744 7816}
7817
85e9cd9a
AMR
7818void bgp_master_init(struct thread_master *master, const int buffer_size,
7819 struct list *addresses)
718e3744 7820{
d62a17ae 7821 qobj_init();
0e64d123 7822
d62a17ae 7823 memset(&bgp_master, 0, sizeof(struct bgp_master));
718e3744 7824
d62a17ae 7825 bm = &bgp_master;
7826 bm->bgp = list_new();
7827 bm->listen_sockets = list_new();
7828 bm->port = BGP_PORT_DEFAULT;
85e9cd9a 7829 bm->addresses = addresses;
d62a17ae 7830 bm->master = master;
7831 bm->start_time = bgp_clock();
7832 bm->t_rmap_update = NULL;
7833 bm->rmap_update_timer = RMAP_DEFAULT_UPDATE_TIMER;
d70583f7
D
7834 bm->v_update_delay = BGP_UPDATE_DELAY_DEF;
7835 bm->v_establish_wait = BGP_UPDATE_DELAY_DEF;
97b4a0ec 7836 bm->terminating = false;
c2d020ad 7837 bm->socket_buffer = buffer_size;
9acb67cb 7838 bm->wait_for_fib = false;
495f0b13 7839
48ecf8f5 7840 bgp_mac_init();
e9eb5f63 7841 /* init the rd id space.
7842 assign 0th index in the bitfield,
523cafc4 7843 so that we start with id 1
7844 */
e9eb5f63 7845 bf_init(bm->rd_idspace, UINT16_MAX);
7846 bf_assign_zero_index(bm->rd_idspace);
7847
955bfd98 7848 /* mpls label dynamic allocation pool */
e70e9f8e 7849 bgp_lp_init(bm->master, &bm->labelpool);
955bfd98 7850
c589d847 7851 bgp_l3nhg_init();
c44ab6f1 7852 bgp_evpn_mh_init();
d62a17ae 7853 QOBJ_REG(bm, bgp_master);
718e3744 7854}
200df115 7855
ad4cbda1 7856/*
009b18fc 7857 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7858 * instance delete (non-default only) or BGP exit.
ad4cbda1 7859 */
d62a17ae 7860static void bgp_if_finish(struct bgp *bgp)
ad4cbda1 7861{
ee66b77f 7862 struct vrf *vrf;
d62a17ae 7863 struct interface *ifp;
7864
ee66b77f
PG
7865 vrf = bgp_vrf_lookup_by_instance_type(bgp);
7866
f4e14fdb 7867 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW || !vrf)
d62a17ae 7868 return;
ad4cbda1 7869
451fda4f 7870 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 7871 struct listnode *c_node, *c_nnode;
7872 struct connected *c;
ad4cbda1 7873
d62a17ae 7874 for (ALL_LIST_ELEMENTS(ifp->connected, c_node, c_nnode, c))
7875 bgp_connected_delete(bgp, c);
7876 }
ad4cbda1 7877}
6b0655a2 7878
d62a17ae 7879static void bgp_viewvrf_autocomplete(vector comps, struct cmd_token *token)
18c57037 7880{
d62a17ae 7881 struct vrf *vrf = NULL;
7882 struct listnode *next;
7883 struct bgp *bgp;
18c57037 7884
6a8ca00f
PG
7885 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
7886 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
18c57037 7887
d62a17ae 7888 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
7889 if (bgp->inst_type != BGP_INSTANCE_TYPE_VIEW)
7890 continue;
18c57037 7891
d62a17ae 7892 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, bgp->name));
7893 }
18c57037
DS
7894}
7895
2ed9fe4a
DL
7896static void bgp_instasn_autocomplete(vector comps, struct cmd_token *token)
7897{
7898 struct listnode *next, *next2;
7899 struct bgp *bgp, *bgp2;
7900 char buf[11];
7901
7902 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
7903 /* deduplicate */
7904 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next2, bgp2)) {
7905 if (bgp2->as == bgp->as)
7906 break;
7907 if (bgp2 == bgp)
7908 break;
7909 }
7910 if (bgp2 != bgp)
7911 continue;
7912
7913 snprintf(buf, sizeof(buf), "%u", bgp->as);
7914 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, buf));
7915 }
7916}
7917
18c57037 7918static const struct cmd_variable_handler bgp_viewvrf_var_handlers[] = {
d62a17ae 7919 {.tokenname = "VIEWVRFNAME", .completions = bgp_viewvrf_autocomplete},
2ed9fe4a 7920 {.varname = "instasn", .completions = bgp_instasn_autocomplete},
d62a17ae 7921 {.completions = NULL},
18c57037
DS
7922};
7923
1ac267a2
DL
7924struct frr_pthread *bgp_pth_io;
7925struct frr_pthread *bgp_pth_ka;
7926
4d762f26 7927static void bgp_pthreads_init(void)
2d4ee774 7928{
1ac267a2
DL
7929 assert(!bgp_pth_io);
7930 assert(!bgp_pth_ka);
7931
a715eab3 7932 struct frr_pthread_attr io = {
a715eab3
QY
7933 .start = frr_pthread_attr_default.start,
7934 .stop = frr_pthread_attr_default.stop,
a715eab3
QY
7935 };
7936 struct frr_pthread_attr ka = {
a715eab3
QY
7937 .start = bgp_keepalives_start,
7938 .stop = bgp_keepalives_stop,
a715eab3 7939 };
1ac267a2
DL
7940 bgp_pth_io = frr_pthread_new(&io, "BGP I/O thread", "bgpd_io");
7941 bgp_pth_ka = frr_pthread_new(&ka, "BGP Keepalives thread", "bgpd_ka");
419dfe6a
QY
7942}
7943
4d762f26 7944void bgp_pthreads_run(void)
419dfe6a 7945{
1ac267a2
DL
7946 frr_pthread_run(bgp_pth_io, NULL);
7947 frr_pthread_run(bgp_pth_ka, NULL);
88b24dee 7948
a715eab3 7949 /* Wait until threads are ready. */
1ac267a2
DL
7950 frr_pthread_wait_running(bgp_pth_io);
7951 frr_pthread_wait_running(bgp_pth_ka);
2d4ee774
QY
7952}
7953
4d762f26 7954void bgp_pthreads_finish(void)
2d4ee774 7955{
0ca8b79f 7956 frr_pthread_stop_all();
2d4ee774
QY
7957}
7958
aa24a36a
DA
7959static int peer_unshut_after_cfg(struct bgp *bgp)
7960{
7961 struct listnode *node;
7962 struct peer *peer;
7963
7964 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
7965 if (!peer->shut_during_cfg)
7966 continue;
7967
7968 if (bgp_debug_neighbor_events(peer))
7969 zlog_debug("%s: released from config-pending hold",
7970 peer->host);
7971
7972 peer->shut_during_cfg = false;
7973 if (peer_active(peer) && peer->status != Established) {
7974 if (peer->status != Idle)
7975 BGP_EVENT_ADD(peer, BGP_Stop);
7976 BGP_EVENT_ADD(peer, BGP_Start);
7977 }
7978 }
7979
7980 return 0;
7981}
7982
f533be73 7983void bgp_init(unsigned short instance)
718e3744 7984{
aa24a36a 7985 hook_register(bgp_config_end, peer_unshut_after_cfg);
fc9a856f 7986
d62a17ae 7987 /* allocates some vital data structures used by peer commands in
7988 * vty_init */
718e3744 7989
419dfe6a
QY
7990 /* pre-init pthreads */
7991 bgp_pthreads_init();
7992
d62a17ae 7993 /* Init zebra. */
f533be73 7994 bgp_zebra_init(bm->master, instance);
718e3744 7995
49e5a4a0 7996#ifdef ENABLE_BGP_VNC
d62a17ae 7997 vnc_zebra_init(bm->master);
65efcfce
LB
7998#endif
7999
d62a17ae 8000 /* BGP VTY commands installation. */
8001 bgp_vty_init();
8002
8003 /* BGP inits. */
8004 bgp_attr_init();
8005 bgp_debug_init();
ed0e57e3 8006 bgp_community_alias_init();
d62a17ae 8007 bgp_dump_init();
8008 bgp_route_init();
8009 bgp_route_map_init();
8010 bgp_scan_vty_init();
8011 bgp_mplsvpn_init();
49e5a4a0 8012#ifdef ENABLE_BGP_VNC
d62a17ae 8013 rfapi_init();
65efcfce 8014#endif
d62a17ae 8015 bgp_ethernetvpn_init();
7c40bf39 8016 bgp_flowspec_vty_init();
d62a17ae 8017
8018 /* Access list initialize. */
8019 access_list_init();
8020 access_list_add_hook(peer_distribute_update);
8021 access_list_delete_hook(peer_distribute_update);
8022
8023 /* Filter list initialize. */
8024 bgp_filter_init();
8025 as_list_add_hook(peer_aslist_add);
8026 as_list_delete_hook(peer_aslist_del);
8027
8028 /* Prefix list initialize.*/
8029 prefix_list_init();
8030 prefix_list_add_hook(peer_prefix_list_update);
8031 prefix_list_delete_hook(peer_prefix_list_update);
8032
8033 /* Community list initialize. */
8034 bgp_clist = community_list_init();
8035
8036 /* BFD init */
21bfce98 8037 bgp_bfd_init(bm->master);
d62a17ae 8038
e3ea6503
PR
8039 bgp_lp_vty_init();
8040
d62a17ae 8041 cmd_variable_handler_register(bgp_viewvrf_var_handlers);
8042}
8043
8044void bgp_terminate(void)
8045{
8046 struct bgp *bgp;
8047 struct peer *peer;
8048 struct listnode *node, *nnode;
8049 struct listnode *mnode, *mnnode;
8050
8051 QOBJ_UNREG(bm);
8052
8053 /* Close the listener sockets first as this prevents peers from
8054 * attempting
8055 * to reconnect on receiving the peer unconfig message. In the presence
8056 * of a large number of peers this will ensure that no peer is left with
8057 * a dangling connection
8058 */
8059 /* reverse bgp_master_init */
8060 bgp_close();
c3004bc4 8061
d62a17ae 8062 if (bm->listen_sockets)
6a154c88 8063 list_delete(&bm->listen_sockets);
d62a17ae 8064
8065 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp))
8066 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
feb17238 8067 if (peer_established(peer) || peer->status == OpenSent
d62a17ae 8068 || peer->status == OpenConfirm)
8069 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
8070 BGP_NOTIFY_CEASE_PEER_UNCONFIG);
8071
22472fee 8072 BGP_TIMER_OFF(bm->t_rmap_update);
955bfd98 8073
48ecf8f5 8074 bgp_mac_finish();
545acafb 8075}
dcc1615e
DD
8076
8077struct peer *peer_lookup_in_view(struct vty *vty, struct bgp *bgp,
8078 const char *ip_str, bool use_json)
8079{
8080 int ret;
8081 struct peer *peer;
8082 union sockunion su;
8083
8084 /* Get peer sockunion. */
8085 ret = str2sockunion(ip_str, &su);
8086 if (ret < 0) {
8087 peer = peer_lookup_by_conf_if(bgp, ip_str);
8088 if (!peer) {
8089 peer = peer_lookup_by_hostname(bgp, ip_str);
8090
8091 if (!peer) {
8092 if (use_json) {
8093 json_object *json_no = NULL;
8094 json_no = json_object_new_object();
8095 json_object_string_add(
8096 json_no,
8097 "malformedAddressOrName",
8098 ip_str);
75eeda93 8099 vty_json(vty, json_no);
dcc1615e
DD
8100 } else
8101 vty_out(vty,
8102 "%% Malformed address or name: %s\n",
8103 ip_str);
8104 return NULL;
8105 }
8106 }
8107 return peer;
8108 }
8109
8110 /* Peer structure lookup. */
8111 peer = peer_lookup(bgp, &su);
8112 if (!peer) {
8113 if (use_json) {
8114 json_object *json_no = NULL;
8115 json_no = json_object_new_object();
8116 json_object_string_add(json_no, "warning",
8117 "No such neighbor in this view/vrf");
75eeda93 8118 vty_json(vty, json_no);
dcc1615e 8119 } else
1c49e813 8120 vty_out(vty, "No such neighbor in this view/vrf\n");
dcc1615e
DD
8121 return NULL;
8122 }
8123
8124 return peer;
8125}
8126
794b37d5 8127void bgp_gr_apply_running_config(void)
8128{
8129 struct peer *peer = NULL;
8130 struct bgp *bgp = NULL;
8131 struct listnode *node, *nnode;
dc95985f 8132 bool gr_router_detected = false;
794b37d5 8133
8134 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
36235319 8135 zlog_debug("[BGP_GR] %s called !", __func__);
794b37d5 8136
dc95985f 8137 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
8138 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
794b37d5 8139 bgp_peer_gr_flags_update(peer);
36235319 8140 if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART))
dc95985f 8141 gr_router_detected = true;
8142 }
8143
36235319
QY
8144 if (gr_router_detected
8145 && bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) {
2ba1fe69 8146 bgp_zebra_send_capabilities(bgp, true);
36235319
QY
8147 } else if (!gr_router_detected
8148 && bgp->present_zebra_gr_state == ZEBRA_GR_ENABLE) {
2ba1fe69 8149 bgp_zebra_send_capabilities(bgp, false);
dc95985f 8150 }
8151
8152 gr_router_detected = false;
8153 }
794b37d5 8154}
f70c91dc
DA
8155
8156printfrr_ext_autoreg_p("BP", printfrr_bp);
8157static ssize_t printfrr_bp(struct fbuf *buf, struct printfrr_eargs *ea,
8158 const void *ptr)
8159{
8160 const struct peer *peer = ptr;
8161
8162 if (!peer)
8163 return bputs(buf, "(null)");
8164
8165 return bprintfrr(buf, "%s(%s)", peer->host,
8166 peer->hostname ? peer->hostname : "Unknown");
8167}