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