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