]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgpd.c
Merge pull request #12562 from opensourcerouting/fix/add_frrtrace_points_for_peer_loc...
[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__, name);
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 /* If peer is configured at least one address family return 1. */
4207 bool peer_active(struct peer *peer)
4208 {
4209 if (BGP_PEER_SU_UNSPEC(peer))
4210 return false;
4211 if (peer->afc[AFI_IP][SAFI_UNICAST] || peer->afc[AFI_IP][SAFI_MULTICAST]
4212 || peer->afc[AFI_IP][SAFI_LABELED_UNICAST]
4213 || peer->afc[AFI_IP][SAFI_MPLS_VPN] || peer->afc[AFI_IP][SAFI_ENCAP]
4214 || peer->afc[AFI_IP][SAFI_FLOWSPEC]
4215 || peer->afc[AFI_IP6][SAFI_UNICAST]
4216 || peer->afc[AFI_IP6][SAFI_MULTICAST]
4217 || peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]
4218 || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
4219 || peer->afc[AFI_IP6][SAFI_ENCAP]
4220 || peer->afc[AFI_IP6][SAFI_FLOWSPEC]
4221 || peer->afc[AFI_L2VPN][SAFI_EVPN])
4222 return true;
4223 return false;
4224 }
4225
4226 /* If peer is negotiated at least one address family return 1. */
4227 bool peer_active_nego(struct peer *peer)
4228 {
4229 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
4230 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
4231 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
4232 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
4233 || peer->afc_nego[AFI_IP][SAFI_ENCAP]
4234 || peer->afc_nego[AFI_IP][SAFI_FLOWSPEC]
4235 || peer->afc_nego[AFI_IP6][SAFI_UNICAST]
4236 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
4237 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
4238 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
4239 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]
4240 || peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC]
4241 || peer->afc_nego[AFI_L2VPN][SAFI_EVPN])
4242 return true;
4243 return false;
4244 }
4245
4246 /* If peer received at least one address family MP, return true */
4247 bool peer_afc_received(struct peer *peer)
4248 {
4249 afi_t afi;
4250 safi_t safi;
4251
4252 FOREACH_AFI_SAFI (afi, safi)
4253 if (peer->afc_recv[afi][safi])
4254 return true;
4255
4256 return false;
4257 }
4258
4259 /* If peer advertised at least one address family MP, return true */
4260 bool peer_afc_advertised(struct peer *peer)
4261 {
4262 afi_t afi;
4263 safi_t safi;
4264
4265 FOREACH_AFI_SAFI (afi, safi)
4266 if (peer->afc_adv[afi][safi])
4267 return true;
4268
4269 return false;
4270 }
4271
4272 void peer_change_action(struct peer *peer, afi_t afi, safi_t safi,
4273 enum peer_change_type type)
4274 {
4275 struct peer_af *paf;
4276
4277 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
4278 return;
4279
4280 if (!peer_established(peer))
4281 return;
4282
4283 if (type == peer_change_reset) {
4284 /* If we're resetting session, we've to delete both peer struct
4285 */
4286 if ((peer->doppelganger)
4287 && (peer->doppelganger->status != Deleted)
4288 && (!CHECK_FLAG(peer->doppelganger->flags,
4289 PEER_FLAG_CONFIG_NODE)))
4290 peer_delete(peer->doppelganger);
4291
4292 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4293 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4294 } else if (type == peer_change_reset_in) {
4295 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
4296 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
4297 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
4298 BGP_ROUTE_REFRESH_NORMAL);
4299 else {
4300 if ((peer->doppelganger)
4301 && (peer->doppelganger->status != Deleted)
4302 && (!CHECK_FLAG(peer->doppelganger->flags,
4303 PEER_FLAG_CONFIG_NODE)))
4304 peer_delete(peer->doppelganger);
4305
4306 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4307 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4308 }
4309 } else if (type == peer_change_reset_out) {
4310 paf = peer_af_find(peer, afi, safi);
4311 if (paf && paf->subgroup)
4312 SET_FLAG(paf->subgroup->sflags,
4313 SUBGRP_STATUS_FORCE_UPDATES);
4314
4315 update_group_adjust_peer(paf);
4316 bgp_announce_route(peer, afi, safi, false);
4317 }
4318 }
4319
4320 struct peer_flag_action {
4321 /* Peer's flag. */
4322 uint64_t flag;
4323
4324 /* This flag can be set for peer-group member. */
4325 uint8_t not_for_member;
4326
4327 /* Action when the flag is changed. */
4328 enum peer_change_type type;
4329 };
4330
4331 static const struct peer_flag_action peer_flag_action_list[] = {
4332 {PEER_FLAG_PASSIVE, 0, peer_change_reset},
4333 {PEER_FLAG_SHUTDOWN, 0, peer_change_reset},
4334 {PEER_FLAG_RTT_SHUTDOWN, 0, peer_change_none},
4335 {PEER_FLAG_DONT_CAPABILITY, 0, peer_change_none},
4336 {PEER_FLAG_OVERRIDE_CAPABILITY, 0, peer_change_none},
4337 {PEER_FLAG_STRICT_CAP_MATCH, 0, peer_change_none},
4338 {PEER_FLAG_DYNAMIC_CAPABILITY, 0, peer_change_reset},
4339 {PEER_FLAG_DISABLE_CONNECTED_CHECK, 0, peer_change_reset},
4340 {PEER_FLAG_CAPABILITY_ENHE, 0, peer_change_reset},
4341 {PEER_FLAG_ENFORCE_FIRST_AS, 0, peer_change_reset_in},
4342 {PEER_FLAG_IFPEER_V6ONLY, 0, peer_change_reset},
4343 {PEER_FLAG_ROUTEADV, 0, peer_change_none},
4344 {PEER_FLAG_TIMER, 0, peer_change_none},
4345 {PEER_FLAG_TIMER_CONNECT, 0, peer_change_none},
4346 {PEER_FLAG_TIMER_DELAYOPEN, 0, peer_change_none},
4347 {PEER_FLAG_PASSWORD, 0, peer_change_none},
4348 {PEER_FLAG_LOCAL_AS, 0, peer_change_reset},
4349 {PEER_FLAG_LOCAL_AS_NO_PREPEND, 0, peer_change_reset},
4350 {PEER_FLAG_LOCAL_AS_REPLACE_AS, 0, peer_change_reset},
4351 {PEER_FLAG_UPDATE_SOURCE, 0, peer_change_none},
4352 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE, 0, peer_change_none},
4353 {PEER_FLAG_EXTENDED_OPT_PARAMS, 0, peer_change_reset},
4354 {PEER_FLAG_ROLE_STRICT_MODE, 0, peer_change_reset},
4355 {PEER_FLAG_ROLE, 0, peer_change_reset},
4356 {PEER_FLAG_PORT, 0, peer_change_reset},
4357 {PEER_FLAG_AIGP, 0, peer_change_none},
4358 {PEER_FLAG_GRACEFUL_SHUTDOWN, 0, peer_change_none},
4359 {0, 0, 0}};
4360
4361 static const struct peer_flag_action peer_af_flag_action_list[] = {
4362 {PEER_FLAG_SEND_COMMUNITY, 1, peer_change_reset_out},
4363 {PEER_FLAG_SEND_EXT_COMMUNITY, 1, peer_change_reset_out},
4364 {PEER_FLAG_SEND_LARGE_COMMUNITY, 1, peer_change_reset_out},
4365 {PEER_FLAG_NEXTHOP_SELF, 1, peer_change_reset_out},
4366 {PEER_FLAG_REFLECTOR_CLIENT, 1, peer_change_reset},
4367 {PEER_FLAG_RSERVER_CLIENT, 1, peer_change_reset},
4368 {PEER_FLAG_SOFT_RECONFIG, 0, peer_change_reset_in},
4369 {PEER_FLAG_AS_PATH_UNCHANGED, 1, peer_change_reset_out},
4370 {PEER_FLAG_NEXTHOP_UNCHANGED, 1, peer_change_reset_out},
4371 {PEER_FLAG_MED_UNCHANGED, 1, peer_change_reset_out},
4372 {PEER_FLAG_DEFAULT_ORIGINATE, 0, peer_change_none},
4373 {PEER_FLAG_REMOVE_PRIVATE_AS, 1, peer_change_reset_out},
4374 {PEER_FLAG_ALLOWAS_IN, 0, peer_change_reset_in},
4375 {PEER_FLAG_ALLOWAS_IN_ORIGIN, 0, peer_change_reset_in},
4376 {PEER_FLAG_ORF_PREFIX_SM, 1, peer_change_reset},
4377 {PEER_FLAG_ORF_PREFIX_RM, 1, peer_change_reset},
4378 {PEER_FLAG_MAX_PREFIX, 0, peer_change_none},
4379 {PEER_FLAG_MAX_PREFIX_WARNING, 0, peer_change_none},
4380 {PEER_FLAG_MAX_PREFIX_FORCE, 0, peer_change_none},
4381 {PEER_FLAG_MAX_PREFIX_OUT, 0, peer_change_none},
4382 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED, 0, peer_change_reset_out},
4383 {PEER_FLAG_FORCE_NEXTHOP_SELF, 1, peer_change_reset_out},
4384 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL, 1, peer_change_reset_out},
4385 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE, 1, peer_change_reset_out},
4386 {PEER_FLAG_AS_OVERRIDE, 1, peer_change_reset_out},
4387 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE, 1, peer_change_reset_out},
4388 {PEER_FLAG_WEIGHT, 0, peer_change_reset_in},
4389 {PEER_FLAG_DISABLE_ADDPATH_RX, 0, peer_change_reset},
4390 {PEER_FLAG_SOO, 0, peer_change_reset},
4391 {PEER_FLAG_ACCEPT_OWN, 0, peer_change_reset},
4392 {0, 0, 0}};
4393
4394 /* Proper action set. */
4395 static int peer_flag_action_set(const struct peer_flag_action *action_list,
4396 int size, struct peer_flag_action *action,
4397 uint64_t flag)
4398 {
4399 int i;
4400 int found = 0;
4401 int reset_in = 0;
4402 int reset_out = 0;
4403 const struct peer_flag_action *match = NULL;
4404
4405 /* Check peer's frag action. */
4406 for (i = 0; i < size; i++) {
4407 match = &action_list[i];
4408
4409 if (match->flag == 0)
4410 break;
4411
4412 if (match->flag & flag) {
4413 found = 1;
4414
4415 if (match->type == peer_change_reset_in)
4416 reset_in = 1;
4417 if (match->type == peer_change_reset_out)
4418 reset_out = 1;
4419 if (match->type == peer_change_reset) {
4420 reset_in = 1;
4421 reset_out = 1;
4422 }
4423 if (match->not_for_member)
4424 action->not_for_member = 1;
4425 }
4426 }
4427
4428 /* Set peer clear type. */
4429 if (reset_in && reset_out)
4430 action->type = peer_change_reset;
4431 else if (reset_in)
4432 action->type = peer_change_reset_in;
4433 else if (reset_out)
4434 action->type = peer_change_reset_out;
4435 else
4436 action->type = peer_change_none;
4437
4438 return found;
4439 }
4440
4441 static void peer_flag_modify_action(struct peer *peer, uint32_t flag)
4442 {
4443 if (flag == PEER_FLAG_SHUTDOWN) {
4444 if (CHECK_FLAG(peer->flags, flag)) {
4445 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT))
4446 peer_nsf_stop(peer);
4447
4448 UNSET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
4449
4450 if (peer->t_pmax_restart) {
4451 THREAD_OFF(peer->t_pmax_restart);
4452 if (bgp_debug_neighbor_events(peer))
4453 zlog_debug(
4454 "%pBP Maximum-prefix restart timer canceled",
4455 peer);
4456 }
4457
4458 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4459 char *msg = peer->tx_shutdown_message;
4460 size_t msglen;
4461 uint8_t msgbuf[BGP_ADMIN_SHUTDOWN_MSG_LEN + 1];
4462
4463 if (!msg && peer_group_active(peer))
4464 msg = peer->group->conf
4465 ->tx_shutdown_message;
4466 msglen = msg ? strlen(msg) : 0;
4467 if (msglen > BGP_ADMIN_SHUTDOWN_MSG_LEN)
4468 msglen = BGP_ADMIN_SHUTDOWN_MSG_LEN;
4469
4470 if (msglen) {
4471 msgbuf[0] = msglen;
4472 memcpy(msgbuf + 1, msg, msglen);
4473
4474 bgp_notify_send_with_data(
4475 peer, BGP_NOTIFY_CEASE,
4476 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN,
4477 msgbuf, msglen + 1);
4478 } else
4479 bgp_notify_send(
4480 peer, BGP_NOTIFY_CEASE,
4481 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
4482 } else
4483 bgp_session_reset(peer);
4484 } else {
4485 peer->v_start = BGP_INIT_START_TIMER;
4486 BGP_EVENT_ADD(peer, BGP_Stop);
4487 }
4488 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4489 if (flag == PEER_FLAG_DYNAMIC_CAPABILITY)
4490 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4491 else if (flag == PEER_FLAG_PASSIVE)
4492 peer->last_reset = PEER_DOWN_PASSIVE_CHANGE;
4493 else if (flag == PEER_FLAG_DISABLE_CONNECTED_CHECK)
4494 peer->last_reset = PEER_DOWN_MULTIHOP_CHANGE;
4495
4496 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4497 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4498 } else
4499 bgp_session_reset(peer);
4500 }
4501
4502 /* Enable global administrative shutdown of all peers of BGP instance */
4503 void bgp_shutdown_enable(struct bgp *bgp, const char *msg)
4504 {
4505 struct peer *peer;
4506 struct listnode *node;
4507 /* length(1) + message(N) */
4508 uint8_t data[BGP_ADMIN_SHUTDOWN_MSG_LEN + 1];
4509
4510 /* do nothing if already shut down */
4511 if (CHECK_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN))
4512 return;
4513
4514 /* informational log message */
4515 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4516 bgp->as);
4517
4518 /* iterate through peers of BGP instance */
4519 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
4520 /* continue, if peer is already in administrative shutdown. */
4521 if (CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN))
4522 continue;
4523
4524 /* send a RFC 4486 notification message if necessary */
4525 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4526 if (msg) {
4527 size_t datalen = strlen(msg);
4528
4529 if (datalen > BGP_ADMIN_SHUTDOWN_MSG_LEN)
4530 datalen = BGP_ADMIN_SHUTDOWN_MSG_LEN;
4531
4532 data[0] = datalen;
4533 memcpy(data + 1, msg, datalen);
4534
4535 bgp_notify_send_with_data(
4536 peer, BGP_NOTIFY_CEASE,
4537 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN, data,
4538 datalen + 1);
4539 } else {
4540 bgp_notify_send(
4541 peer, BGP_NOTIFY_CEASE,
4542 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
4543 }
4544 }
4545
4546 /* reset start timer to initial value */
4547 peer->v_start = BGP_INIT_START_TIMER;
4548
4549 /* trigger a RFC 4271 ManualStop event */
4550 BGP_EVENT_ADD(peer, BGP_Stop);
4551 }
4552
4553 /* set the BGP instances shutdown flag */
4554 SET_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN);
4555 }
4556
4557 /* Disable global administrative shutdown of all peers of BGP instance */
4558 void bgp_shutdown_disable(struct bgp *bgp)
4559 {
4560 /* do nothing if not shut down. */
4561 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN))
4562 return;
4563
4564 /* informational log message */
4565 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4566 bgp->as);
4567
4568 /* clear the BGP instances shutdown flag */
4569 UNSET_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN);
4570 }
4571
4572 /* Change specified peer flag. */
4573 static int peer_flag_modify(struct peer *peer, uint64_t flag, int set)
4574 {
4575 int found;
4576 int size;
4577 bool invert, member_invert;
4578 struct peer *member;
4579 struct listnode *node, *nnode;
4580 struct peer_flag_action action;
4581
4582 memset(&action, 0, sizeof(struct peer_flag_action));
4583 size = sizeof(peer_flag_action_list) / sizeof(struct peer_flag_action);
4584
4585 invert = CHECK_FLAG(peer->flags_invert, flag);
4586 found = peer_flag_action_set(peer_flag_action_list, size, &action,
4587 flag);
4588
4589 /* Abort if no flag action exists. */
4590 if (!found)
4591 return BGP_ERR_INVALID_FLAG;
4592
4593 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4594 if (set && CHECK_FLAG(peer->flags | flag, PEER_FLAG_STRICT_CAP_MATCH)
4595 && CHECK_FLAG(peer->flags | flag, PEER_FLAG_OVERRIDE_CAPABILITY))
4596 return BGP_ERR_PEER_FLAG_CONFLICT;
4597
4598 /* Handle flag updates where desired state matches current state. */
4599 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4600 if (set && CHECK_FLAG(peer->flags, flag)) {
4601 COND_FLAG(peer->flags_override, flag, !invert);
4602 return 0;
4603 }
4604
4605 if (!set && !CHECK_FLAG(peer->flags, flag)) {
4606 COND_FLAG(peer->flags_override, flag, invert);
4607 return 0;
4608 }
4609 }
4610
4611 /* Inherit from peer-group or set/unset flags accordingly. */
4612 if (peer_group_active(peer) && set == invert)
4613 peer_flag_inherit(peer, flag);
4614 else
4615 COND_FLAG(peer->flags, flag, set);
4616
4617 /* Check if handling a regular peer. */
4618 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4619 /* Update flag override state accordingly. */
4620 COND_FLAG(peer->flags_override, flag, set != invert);
4621
4622 /*
4623 * For the extended next-hop encoding flag we need to turn RAs
4624 * on if flag is being set, but only turn RAs off if the flag
4625 * is being unset on this peer and if this peer is a member of a
4626 * peer-group, the peer-group also doesn't have the flag set.
4627 */
4628 if (flag == PEER_FLAG_CAPABILITY_ENHE) {
4629 if (set) {
4630 bgp_zebra_initiate_radv(peer->bgp, peer);
4631 } else if (peer_group_active(peer)) {
4632 if (!CHECK_FLAG(peer->group->conf->flags,
4633 flag) &&
4634 !peer->conf_if)
4635 bgp_zebra_terminate_radv(peer->bgp,
4636 peer);
4637 } else
4638 bgp_zebra_terminate_radv(peer->bgp, peer);
4639 }
4640
4641 /* Execute flag action on peer. */
4642 if (action.type == peer_change_reset)
4643 peer_flag_modify_action(peer, flag);
4644
4645 /* Skip peer-group mechanics for regular peers. */
4646 return 0;
4647 }
4648
4649 /*
4650 * Update peer-group members, unless they are explicitly overriding
4651 * peer-group configuration.
4652 */
4653 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
4654 /* Skip peers with overridden configuration. */
4655 if (CHECK_FLAG(member->flags_override, flag))
4656 continue;
4657
4658 /* Check if only member without group is inverted. */
4659 member_invert =
4660 CHECK_FLAG(member->flags_invert, flag) && !invert;
4661
4662 /* Skip peers with equivalent configuration. */
4663 if (set != member_invert && CHECK_FLAG(member->flags, flag))
4664 continue;
4665
4666 if (set == member_invert && !CHECK_FLAG(member->flags, flag))
4667 continue;
4668
4669 /* Update flag on peer-group member. */
4670 COND_FLAG(member->flags, flag, set != member_invert);
4671
4672 if (flag == PEER_FLAG_CAPABILITY_ENHE && !member->conf_if)
4673 set ? bgp_zebra_initiate_radv(member->bgp, member)
4674 : bgp_zebra_terminate_radv(member->bgp, member);
4675
4676 /* Execute flag action on peer-group member. */
4677 if (action.type == peer_change_reset)
4678 peer_flag_modify_action(member, flag);
4679 }
4680
4681 return 0;
4682 }
4683
4684 int peer_flag_set(struct peer *peer, uint64_t flag)
4685 {
4686 return peer_flag_modify(peer, flag, 1);
4687 }
4688
4689 int peer_flag_unset(struct peer *peer, uint64_t flag)
4690 {
4691 return peer_flag_modify(peer, flag, 0);
4692 }
4693
4694 static int peer_af_flag_modify(struct peer *peer, afi_t afi, safi_t safi,
4695 uint64_t flag, bool set)
4696 {
4697 int found;
4698 int size;
4699 bool invert, member_invert;
4700 struct peer *member;
4701 struct listnode *node, *nnode;
4702 struct peer_flag_action action;
4703 enum bgp_peer_sort ptype;
4704
4705 memset(&action, 0, sizeof(struct peer_flag_action));
4706 size = sizeof(peer_af_flag_action_list)
4707 / sizeof(struct peer_flag_action);
4708
4709 invert = CHECK_FLAG(peer->af_flags_invert[afi][safi], flag);
4710 found = peer_flag_action_set(peer_af_flag_action_list, size, &action,
4711 flag);
4712
4713 /* Abort if flag action exists. */
4714 if (!found)
4715 return BGP_ERR_INVALID_FLAG;
4716
4717 ptype = peer_sort(peer);
4718 /* Special check for reflector client. */
4719 if (flag & PEER_FLAG_REFLECTOR_CLIENT && ptype != BGP_PEER_IBGP)
4720 return BGP_ERR_NOT_INTERNAL_PEER;
4721
4722 /* Do not remove reflector client when ORR is configured on this peer */
4723 if (flag & PEER_FLAG_REFLECTOR_CLIENT && !set &&
4724 peer_orr_rrclient_check(peer, afi, safi))
4725 return BGP_ERR_PEER_ORR_CONFIGURED;
4726
4727 /* Special check for remove-private-AS. */
4728 if (flag & PEER_FLAG_REMOVE_PRIVATE_AS && ptype == BGP_PEER_IBGP)
4729 return BGP_ERR_REMOVE_PRIVATE_AS;
4730
4731 /* as-override is not allowed for IBGP peers */
4732 if (flag & PEER_FLAG_AS_OVERRIDE && ptype == BGP_PEER_IBGP)
4733 return BGP_ERR_AS_OVERRIDE;
4734
4735 /* Handle flag updates where desired state matches current state. */
4736 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4737 if (set && CHECK_FLAG(peer->af_flags[afi][safi], flag)) {
4738 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4739 !invert);
4740 return 0;
4741 }
4742
4743 if (!set && !CHECK_FLAG(peer->af_flags[afi][safi], flag)) {
4744 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4745 invert);
4746 return 0;
4747 }
4748 }
4749
4750 /*
4751 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4752 * if we are setting/unsetting flags which conflict with this flag
4753 * handle accordingly
4754 */
4755 if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
4756 if (set) {
4757
4758 /*
4759 * if we are setting NEXTHOP_SELF, we need to unset the
4760 * NEXTHOP_UNCHANGED flag
4761 */
4762 if (CHECK_FLAG(flag, PEER_FLAG_NEXTHOP_SELF) ||
4763 CHECK_FLAG(flag, PEER_FLAG_FORCE_NEXTHOP_SELF))
4764 UNSET_FLAG(peer->af_flags[afi][safi],
4765 PEER_FLAG_NEXTHOP_UNCHANGED);
4766 } else {
4767
4768 /*
4769 * if we are unsetting NEXTHOP_SELF, we need to set the
4770 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4771 */
4772 if (CHECK_FLAG(flag, PEER_FLAG_NEXTHOP_SELF) ||
4773 CHECK_FLAG(flag, PEER_FLAG_FORCE_NEXTHOP_SELF))
4774 SET_FLAG(peer->af_flags[afi][safi],
4775 PEER_FLAG_NEXTHOP_UNCHANGED);
4776 }
4777 }
4778
4779 /*
4780 * If the peer is a route server client let's not
4781 * muck with the nexthop on the way out the door
4782 */
4783 if (flag & PEER_FLAG_RSERVER_CLIENT) {
4784 if (set)
4785 SET_FLAG(peer->af_flags[afi][safi],
4786 PEER_FLAG_NEXTHOP_UNCHANGED);
4787 else
4788 UNSET_FLAG(peer->af_flags[afi][safi],
4789 PEER_FLAG_NEXTHOP_UNCHANGED);
4790 }
4791
4792 /* Inherit from peer-group or set/unset flags accordingly. */
4793 if (peer_group_active(peer) && set == invert)
4794 peer_af_flag_inherit(peer, afi, safi, flag);
4795 else
4796 COND_FLAG(peer->af_flags[afi][safi], flag, set);
4797
4798 /* Execute action when peer is established. */
4799 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
4800 && peer_established(peer)) {
4801 if (!set && flag == PEER_FLAG_SOFT_RECONFIG)
4802 bgp_clear_adj_in(peer, afi, safi);
4803 else {
4804 if (flag == PEER_FLAG_REFLECTOR_CLIENT)
4805 peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE;
4806 else if (flag == PEER_FLAG_RSERVER_CLIENT)
4807 peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE;
4808 else if (flag == PEER_FLAG_ORF_PREFIX_SM)
4809 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4810 else if (flag == PEER_FLAG_ORF_PREFIX_RM)
4811 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4812
4813 peer_change_action(peer, afi, safi, action.type);
4814 }
4815 }
4816
4817 /* Check if handling a regular peer. */
4818 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4819 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4820 set != invert);
4821 } else {
4822 /*
4823 * Update peer-group members, unless they are explicitly
4824 * overriding peer-group configuration.
4825 */
4826 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode,
4827 member)) {
4828 /* Skip peers with overridden configuration. */
4829 if (CHECK_FLAG(member->af_flags_override[afi][safi],
4830 flag))
4831 continue;
4832
4833 /* Check if only member without group is inverted. */
4834 member_invert =
4835 CHECK_FLAG(member->af_flags_invert[afi][safi],
4836 flag)
4837 && !invert;
4838
4839 /* Skip peers with equivalent configuration. */
4840 if (set != member_invert
4841 && CHECK_FLAG(member->af_flags[afi][safi], flag))
4842 continue;
4843
4844 if (set == member_invert
4845 && !CHECK_FLAG(member->af_flags[afi][safi], flag))
4846 continue;
4847
4848 /* Update flag on peer-group member. */
4849 COND_FLAG(member->af_flags[afi][safi], flag,
4850 set != member_invert);
4851
4852 /* Execute flag action on peer-group member. */
4853 if (peer_established(member)) {
4854 if (!set && flag == PEER_FLAG_SOFT_RECONFIG)
4855 bgp_clear_adj_in(member, afi, safi);
4856 else {
4857 if (flag == PEER_FLAG_REFLECTOR_CLIENT)
4858 member->last_reset =
4859 PEER_DOWN_RR_CLIENT_CHANGE;
4860 else if (flag
4861 == PEER_FLAG_RSERVER_CLIENT)
4862 member->last_reset =
4863 PEER_DOWN_RS_CLIENT_CHANGE;
4864 else if (flag
4865 == PEER_FLAG_ORF_PREFIX_SM)
4866 member->last_reset =
4867 PEER_DOWN_CAPABILITY_CHANGE;
4868 else if (flag
4869 == PEER_FLAG_ORF_PREFIX_RM)
4870 member->last_reset =
4871 PEER_DOWN_CAPABILITY_CHANGE;
4872
4873 peer_change_action(member, afi, safi,
4874 action.type);
4875 }
4876 }
4877 }
4878 }
4879
4880 return 0;
4881 }
4882
4883 int peer_af_flag_set(struct peer *peer, afi_t afi, safi_t safi, uint64_t flag)
4884 {
4885 return peer_af_flag_modify(peer, afi, safi, flag, 1);
4886 }
4887
4888 int peer_af_flag_unset(struct peer *peer, afi_t afi, safi_t safi, uint64_t flag)
4889 {
4890 return peer_af_flag_modify(peer, afi, safi, flag, 0);
4891 }
4892
4893
4894 void peer_tx_shutdown_message_set(struct peer *peer, const char *msg)
4895 {
4896 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
4897 peer->tx_shutdown_message =
4898 msg ? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG, msg) : NULL;
4899 }
4900
4901 void peer_tx_shutdown_message_unset(struct peer *peer)
4902 {
4903 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
4904 }
4905
4906
4907 /* EBGP multihop configuration. */
4908 int peer_ebgp_multihop_set(struct peer *peer, int ttl)
4909 {
4910 struct peer_group *group;
4911 struct listnode *node, *nnode;
4912 struct peer *peer1;
4913
4914 if (peer->sort == BGP_PEER_IBGP || peer->conf_if)
4915 return 0;
4916
4917 /* is there anything to do? */
4918 if (peer->ttl == ttl)
4919 return 0;
4920
4921 /* see comment in peer_ttl_security_hops_set() */
4922 if (ttl != MAXTTL) {
4923 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4924 group = peer->group;
4925 if (group->conf->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
4926 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4927
4928 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
4929 peer1)) {
4930 if (peer1->sort == BGP_PEER_IBGP)
4931 continue;
4932
4933 if (peer1->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
4934 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4935 }
4936 } else {
4937 if (peer->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
4938 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4939 }
4940 }
4941
4942 peer->ttl = ttl;
4943
4944 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4945 if (peer->sort != BGP_PEER_IBGP) {
4946 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4947 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4948 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4949 else
4950 bgp_session_reset(peer);
4951
4952 /* Reconfigure BFD peer with new TTL. */
4953 if (peer->bfd_config)
4954 bgp_peer_bfd_update_source(peer);
4955 }
4956 } else {
4957 group = peer->group;
4958 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
4959 if (peer->sort == BGP_PEER_IBGP)
4960 continue;
4961
4962 peer->ttl = group->conf->ttl;
4963
4964 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4965 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4966 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4967 else
4968 bgp_session_reset(peer);
4969
4970 /* Reconfigure BFD peer with new TTL. */
4971 if (peer->bfd_config)
4972 bgp_peer_bfd_update_source(peer);
4973 }
4974 }
4975 return 0;
4976 }
4977
4978 int peer_ebgp_multihop_unset(struct peer *peer)
4979 {
4980 struct peer_group *group;
4981 struct listnode *node, *nnode;
4982 int ttl;
4983
4984 if (peer->sort == BGP_PEER_IBGP)
4985 return 0;
4986
4987 if (peer->gtsm_hops != BGP_GTSM_HOPS_DISABLED && peer->ttl != MAXTTL)
4988 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4989
4990 if (peer_group_active(peer))
4991 ttl = peer->group->conf->ttl;
4992 else
4993 ttl = BGP_DEFAULT_TTL;
4994
4995 if (ttl == peer->ttl)
4996 return 0;
4997
4998 peer->ttl = ttl;
4999
5000 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5001 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
5002 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5003 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5004 else
5005 bgp_session_reset(peer);
5006
5007 /* Reconfigure BFD peer with new TTL. */
5008 if (peer->bfd_config)
5009 bgp_peer_bfd_update_source(peer);
5010 } else {
5011 group = peer->group;
5012 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
5013 if (peer->sort == BGP_PEER_IBGP)
5014 continue;
5015
5016 peer->ttl = BGP_DEFAULT_TTL;
5017
5018 if (peer->fd >= 0) {
5019 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
5020 bgp_notify_send(
5021 peer, BGP_NOTIFY_CEASE,
5022 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5023 else
5024 bgp_session_reset(peer);
5025 }
5026
5027 /* Reconfigure BFD peer with new TTL. */
5028 if (peer->bfd_config)
5029 bgp_peer_bfd_update_source(peer);
5030 }
5031 }
5032 return 0;
5033 }
5034
5035 /* Set Open Policy Role and check its correctness */
5036 int peer_role_set(struct peer *peer, uint8_t role, bool strict_mode)
5037 {
5038 struct peer *member;
5039 struct listnode *node, *nnode;
5040
5041 peer_flag_set(peer, PEER_FLAG_ROLE);
5042
5043 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5044 if (peer->sort != BGP_PEER_EBGP)
5045 return BGP_ERR_INVALID_INTERNAL_ROLE;
5046
5047 if (peer->local_role == role) {
5048 if (CHECK_FLAG(peer->flags,
5049 PEER_FLAG_ROLE_STRICT_MODE) &&
5050 !strict_mode)
5051 /* TODO: Is session restart needed if it was
5052 * down?
5053 */
5054 UNSET_FLAG(peer->flags,
5055 PEER_FLAG_ROLE_STRICT_MODE);
5056 if (!CHECK_FLAG(peer->flags,
5057 PEER_FLAG_ROLE_STRICT_MODE) &&
5058 strict_mode) {
5059 SET_FLAG(peer->flags,
5060 PEER_FLAG_ROLE_STRICT_MODE);
5061 /* Restart session to throw Role Mismatch
5062 * Notification
5063 */
5064 if (peer->remote_role == ROLE_UNDEFINED)
5065 bgp_session_reset(peer);
5066 }
5067 } else {
5068 peer->local_role = role;
5069 if (strict_mode)
5070 SET_FLAG(peer->flags,
5071 PEER_FLAG_ROLE_STRICT_MODE);
5072 else
5073 UNSET_FLAG(peer->flags,
5074 PEER_FLAG_ROLE_STRICT_MODE);
5075 bgp_session_reset(peer);
5076 }
5077
5078 return CMD_SUCCESS;
5079 }
5080
5081 peer->local_role = role;
5082 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5083 if (member->sort != BGP_PEER_EBGP)
5084 return BGP_ERR_INVALID_INTERNAL_ROLE;
5085
5086 if (member->local_role == role) {
5087 if (CHECK_FLAG(member->flags,
5088 PEER_FLAG_ROLE_STRICT_MODE) &&
5089 !strict_mode)
5090 /* TODO: Is session restart needed if it was
5091 * down?
5092 */
5093 UNSET_FLAG(member->flags,
5094 PEER_FLAG_ROLE_STRICT_MODE);
5095 if (!CHECK_FLAG(member->flags,
5096 PEER_FLAG_ROLE_STRICT_MODE) &&
5097 strict_mode) {
5098 SET_FLAG(peer->flags,
5099 PEER_FLAG_ROLE_STRICT_MODE);
5100 SET_FLAG(member->flags,
5101 PEER_FLAG_ROLE_STRICT_MODE);
5102 /* Restart session to throw Role Mismatch
5103 * Notification
5104 */
5105 if (member->remote_role == ROLE_UNDEFINED)
5106 bgp_session_reset(member);
5107 }
5108 } else {
5109 member->local_role = role;
5110
5111 if (strict_mode) {
5112 SET_FLAG(peer->flags,
5113 PEER_FLAG_ROLE_STRICT_MODE);
5114 SET_FLAG(member->flags,
5115 PEER_FLAG_ROLE_STRICT_MODE);
5116 } else {
5117 UNSET_FLAG(member->flags,
5118 PEER_FLAG_ROLE_STRICT_MODE);
5119 }
5120 bgp_session_reset(member);
5121 }
5122 }
5123
5124 return CMD_SUCCESS;
5125 }
5126
5127 int peer_role_unset(struct peer *peer)
5128 {
5129 struct peer *member;
5130 struct listnode *node, *nnode;
5131
5132 peer_flag_unset(peer, PEER_FLAG_ROLE);
5133
5134 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5135 return peer_role_set(peer, ROLE_UNDEFINED, 0);
5136
5137 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member))
5138 peer_role_set(member, ROLE_UNDEFINED, 0);
5139
5140 return CMD_SUCCESS;
5141 }
5142
5143 /* Neighbor description. */
5144 void peer_description_set(struct peer *peer, const char *desc)
5145 {
5146 XFREE(MTYPE_PEER_DESC, peer->desc);
5147
5148 peer->desc = XSTRDUP(MTYPE_PEER_DESC, desc);
5149 }
5150
5151 void peer_description_unset(struct peer *peer)
5152 {
5153 XFREE(MTYPE_PEER_DESC, peer->desc);
5154 }
5155
5156 /* Neighbor update-source. */
5157 int peer_update_source_if_set(struct peer *peer, const char *ifname)
5158 {
5159 struct peer *member;
5160 struct listnode *node, *nnode;
5161
5162 /* Set flag and configuration on peer. */
5163 peer_flag_set(peer, PEER_FLAG_UPDATE_SOURCE);
5164 if (peer->update_if) {
5165 if (strcmp(peer->update_if, ifname) == 0)
5166 return 0;
5167 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
5168 }
5169 peer->update_if = XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, ifname);
5170 sockunion_free(peer->update_source);
5171 peer->update_source = NULL;
5172
5173 /* Check if handling a regular peer. */
5174 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5175 /* Send notification or reset peer depending on state. */
5176 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5177 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5178 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5179 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5180 } else
5181 bgp_session_reset(peer);
5182
5183 /* Apply new source configuration to BFD session. */
5184 if (peer->bfd_config)
5185 bgp_peer_bfd_update_source(peer);
5186
5187 /* Skip peer-group mechanics for regular peers. */
5188 return 0;
5189 }
5190
5191 /*
5192 * Set flag and configuration on all peer-group members, unless they are
5193 * explicitly overriding peer-group configuration.
5194 */
5195 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5196 /* Skip peers with overridden configuration. */
5197 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5198 continue;
5199
5200 /* Skip peers with the same configuration. */
5201 if (member->update_if) {
5202 if (strcmp(member->update_if, ifname) == 0)
5203 continue;
5204 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5205 }
5206
5207 /* Set flag and configuration on peer-group member. */
5208 SET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5209 member->update_if = XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, ifname);
5210 sockunion_free(member->update_source);
5211 member->update_source = NULL;
5212
5213 /* Send notification or reset peer depending on state. */
5214 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5215 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5216 bgp_notify_send(member, BGP_NOTIFY_CEASE,
5217 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5218 } else
5219 bgp_session_reset(member);
5220
5221 /* Apply new source configuration to BFD session. */
5222 if (member->bfd_config)
5223 bgp_peer_bfd_update_source(member);
5224 }
5225
5226 return 0;
5227 }
5228
5229 void peer_update_source_addr_set(struct peer *peer, const union sockunion *su)
5230 {
5231 struct peer *member;
5232 struct listnode *node, *nnode;
5233
5234 /* Set flag and configuration on peer. */
5235 peer_flag_set(peer, PEER_FLAG_UPDATE_SOURCE);
5236 if (peer->update_source) {
5237 if (sockunion_cmp(peer->update_source, su) == 0)
5238 return;
5239 sockunion_free(peer->update_source);
5240 }
5241 peer->update_source = sockunion_dup(su);
5242 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
5243
5244 /* Check if handling a regular peer. */
5245 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5246 /* Send notification or reset peer depending on state. */
5247 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5248 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5249 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5250 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5251 } else
5252 bgp_session_reset(peer);
5253
5254 /* Apply new source configuration to BFD session. */
5255 if (peer->bfd_config)
5256 bgp_peer_bfd_update_source(peer);
5257
5258 /* Skip peer-group mechanics for regular peers. */
5259 return;
5260 }
5261
5262 /*
5263 * Set flag and configuration on all peer-group members, unless they are
5264 * explicitly overriding peer-group configuration.
5265 */
5266 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5267 /* Skip peers with overridden configuration. */
5268 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5269 continue;
5270
5271 /* Skip peers with the same configuration. */
5272 if (member->update_source) {
5273 if (sockunion_cmp(member->update_source, su) == 0)
5274 continue;
5275 sockunion_free(member->update_source);
5276 }
5277
5278 /* Set flag and configuration on peer-group member. */
5279 SET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5280 member->update_source = sockunion_dup(su);
5281 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5282
5283 /* Send notification or reset peer depending on state. */
5284 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5285 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5286 bgp_notify_send(member, BGP_NOTIFY_CEASE,
5287 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5288 } else
5289 bgp_session_reset(member);
5290
5291 /* Apply new source configuration to BFD session. */
5292 if (member->bfd_config)
5293 bgp_peer_bfd_update_source(member);
5294 }
5295 }
5296
5297 void peer_update_source_unset(struct peer *peer)
5298 {
5299 struct peer *member;
5300 struct listnode *node, *nnode;
5301
5302 if (!CHECK_FLAG(peer->flags, PEER_FLAG_UPDATE_SOURCE))
5303 return;
5304
5305 /* Inherit configuration from peer-group if peer is member. */
5306 if (peer_group_active(peer)) {
5307 peer_flag_inherit(peer, PEER_FLAG_UPDATE_SOURCE);
5308 PEER_SU_ATTR_INHERIT(peer, peer->group, update_source);
5309 PEER_STR_ATTR_INHERIT(peer, peer->group, update_if,
5310 MTYPE_PEER_UPDATE_SOURCE);
5311 } else {
5312 /* Otherwise remove flag and configuration from peer. */
5313 peer_flag_unset(peer, PEER_FLAG_UPDATE_SOURCE);
5314 sockunion_free(peer->update_source);
5315 peer->update_source = NULL;
5316 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
5317 }
5318
5319 /* Check if handling a regular peer. */
5320 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5321 /* Send notification or reset peer depending on state. */
5322 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5323 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5324 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5325 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5326 } else
5327 bgp_session_reset(peer);
5328
5329 /* Apply new source configuration to BFD session. */
5330 if (peer->bfd_config)
5331 bgp_peer_bfd_update_source(peer);
5332
5333 /* Skip peer-group mechanics for regular peers. */
5334 return;
5335 }
5336
5337 /*
5338 * Set flag and configuration on all peer-group members, unless they are
5339 * explicitly overriding peer-group configuration.
5340 */
5341 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5342 /* Skip peers with overridden configuration. */
5343 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5344 continue;
5345
5346 /* Skip peers with the same configuration. */
5347 if (!CHECK_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE)
5348 && !member->update_source && !member->update_if)
5349 continue;
5350
5351 /* Remove flag and configuration on peer-group member. */
5352 UNSET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5353 sockunion_free(member->update_source);
5354 member->update_source = NULL;
5355 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5356
5357 /* Send notification or reset peer depending on state. */
5358 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5359 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5360 bgp_notify_send(member, BGP_NOTIFY_CEASE,
5361 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5362 } else
5363 bgp_session_reset(member);
5364
5365 /* Apply new source configuration to BFD session. */
5366 if (member->bfd_config)
5367 bgp_peer_bfd_update_source(member);
5368 }
5369 }
5370
5371 int peer_default_originate_set(struct peer *peer, afi_t afi, safi_t safi,
5372 const char *rmap, struct route_map *route_map)
5373 {
5374 struct peer *member;
5375 struct listnode *node, *nnode;
5376 struct update_subgroup *subgrp;
5377
5378 /* Set flag and configuration on peer. */
5379 peer_af_flag_set(peer, afi, safi, PEER_FLAG_DEFAULT_ORIGINATE);
5380
5381 subgrp = peer_subgroup(peer, afi, safi);
5382
5383 if (rmap) {
5384 if (!peer->default_rmap[afi][safi].name
5385 || strcmp(rmap, peer->default_rmap[afi][safi].name) != 0) {
5386 if (peer->default_rmap[afi][safi].name)
5387 XFREE(MTYPE_ROUTE_MAP_NAME,
5388 peer->default_rmap[afi][safi].name);
5389
5390 /*
5391 * When there is a change in route-map policy,
5392 * this flow gets triggered. Since, the default
5393 * route is already originated, the flag is set.
5394 * The flag should be unset here,
5395 * to trigger the flow of sending update message.
5396 */
5397 if (subgrp)
5398 UNSET_FLAG(subgrp->sflags,
5399 SUBGRP_STATUS_DEFAULT_ORIGINATE);
5400
5401 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
5402 peer->default_rmap[afi][safi].name =
5403 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
5404 peer->default_rmap[afi][safi].map = route_map;
5405 route_map_counter_increment(route_map);
5406 }
5407 } else if (!rmap) {
5408 if (peer->default_rmap[afi][safi].name)
5409 XFREE(MTYPE_ROUTE_MAP_NAME,
5410 peer->default_rmap[afi][safi].name);
5411
5412 /*
5413 * This is triggered in case of route-map deletion.
5414 * The flag needs to be unset, to trigger the flow
5415 * of sending an update message.
5416 */
5417 if (subgrp)
5418 UNSET_FLAG(subgrp->sflags,
5419 SUBGRP_STATUS_DEFAULT_ORIGINATE);
5420
5421 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
5422 peer->default_rmap[afi][safi].name = NULL;
5423 peer->default_rmap[afi][safi].map = NULL;
5424 }
5425
5426 /* Check if handling a regular peer. */
5427 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5428 /* Update peer route announcements. */
5429 if (peer_established(peer) && peer->afc_nego[afi][safi]) {
5430 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5431 bgp_default_originate(peer, afi, safi, 0);
5432 bgp_announce_route(peer, afi, safi, false);
5433 }
5434
5435 /* Skip peer-group mechanics for regular peers. */
5436 return 0;
5437 }
5438
5439 /*
5440 * Set flag and configuration on all peer-group members, unless they are
5441 * explicitly overriding peer-group configuration.
5442 */
5443 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5444 /* Skip peers with overridden configuration. */
5445 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5446 PEER_FLAG_DEFAULT_ORIGINATE))
5447 continue;
5448
5449 /* Set flag and configuration on peer-group member. */
5450 SET_FLAG(member->af_flags[afi][safi],
5451 PEER_FLAG_DEFAULT_ORIGINATE);
5452 if (rmap) {
5453 if (member->default_rmap[afi][safi].name)
5454 XFREE(MTYPE_ROUTE_MAP_NAME,
5455 member->default_rmap[afi][safi].name);
5456 route_map_counter_decrement(
5457 member->default_rmap[afi][safi].map);
5458 member->default_rmap[afi][safi].name =
5459 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
5460 member->default_rmap[afi][safi].map = route_map;
5461 route_map_counter_increment(route_map);
5462 }
5463
5464 /* Update peer route announcements. */
5465 if (peer_established(member) && member->afc_nego[afi][safi]) {
5466 update_group_adjust_peer(
5467 peer_af_find(member, afi, safi));
5468 bgp_default_originate(member, afi, safi, 0);
5469 bgp_announce_route(member, afi, safi, false);
5470 }
5471 }
5472
5473 return 0;
5474 }
5475
5476 int peer_default_originate_unset(struct peer *peer, afi_t afi, safi_t safi)
5477 {
5478 struct peer *member;
5479 struct listnode *node, *nnode;
5480
5481 /* Inherit configuration from peer-group if peer is member. */
5482 if (peer_group_active(peer)) {
5483 peer_af_flag_inherit(peer, afi, safi,
5484 PEER_FLAG_DEFAULT_ORIGINATE);
5485 PEER_STR_ATTR_INHERIT(peer, peer->group,
5486 default_rmap[afi][safi].name,
5487 MTYPE_ROUTE_MAP_NAME);
5488 PEER_ATTR_INHERIT(peer, peer->group,
5489 default_rmap[afi][safi].map);
5490 } else {
5491 /* Otherwise remove flag and configuration from peer. */
5492 peer_af_flag_unset(peer, afi, safi,
5493 PEER_FLAG_DEFAULT_ORIGINATE);
5494 if (peer->default_rmap[afi][safi].name)
5495 XFREE(MTYPE_ROUTE_MAP_NAME,
5496 peer->default_rmap[afi][safi].name);
5497 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
5498 peer->default_rmap[afi][safi].name = NULL;
5499 peer->default_rmap[afi][safi].map = NULL;
5500 }
5501
5502 /* Check if handling a regular peer. */
5503 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5504 /* Update peer route announcements. */
5505 if (peer_established(peer) && peer->afc_nego[afi][safi]) {
5506 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5507 bgp_default_originate(peer, afi, safi, 1);
5508 bgp_announce_route(peer, afi, safi, false);
5509 }
5510
5511 /* Skip peer-group mechanics for regular peers. */
5512 return 0;
5513 }
5514
5515 /*
5516 * Remove flag and configuration from all peer-group members, unless
5517 * they are explicitly overriding peer-group configuration.
5518 */
5519 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5520 /* Skip peers with overridden configuration. */
5521 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5522 PEER_FLAG_DEFAULT_ORIGINATE))
5523 continue;
5524
5525 /* Remove flag and configuration on peer-group member. */
5526 UNSET_FLAG(member->af_flags[afi][safi],
5527 PEER_FLAG_DEFAULT_ORIGINATE);
5528 if (member->default_rmap[afi][safi].name)
5529 XFREE(MTYPE_ROUTE_MAP_NAME,
5530 member->default_rmap[afi][safi].name);
5531 route_map_counter_decrement(member->default_rmap[afi][safi].map);
5532 member->default_rmap[afi][safi].name = NULL;
5533 member->default_rmap[afi][safi].map = NULL;
5534
5535 /* Update peer route announcements. */
5536 if (peer_established(member) && member->afc_nego[afi][safi]) {
5537 update_group_adjust_peer(peer_af_find(member, afi, safi));
5538 bgp_default_originate(member, afi, safi, 1);
5539 bgp_announce_route(member, afi, safi, false);
5540 }
5541 }
5542
5543 return 0;
5544 }
5545
5546 void peer_port_set(struct peer *peer, uint16_t port)
5547 {
5548 peer->port = port;
5549 peer_flag_set(peer, PEER_FLAG_PORT);
5550 }
5551
5552 void peer_port_unset(struct peer *peer)
5553 {
5554 peer->port = BGP_PORT_DEFAULT;
5555 peer_flag_unset(peer, PEER_FLAG_PORT);
5556 }
5557
5558 /* Set the TCP-MSS value in the peer structure,
5559 * This gets applied only after connection reset
5560 * So this value will be used in bgp_connect.
5561 */
5562 void peer_tcp_mss_set(struct peer *peer, uint32_t tcp_mss)
5563 {
5564 peer->tcp_mss = tcp_mss;
5565 SET_FLAG(peer->flags, PEER_FLAG_TCP_MSS);
5566 }
5567
5568 /* Reset the TCP-MSS value in the peer structure,
5569 * This gets applied only after connection reset
5570 * So this value will be used in bgp_connect.
5571 */
5572 void peer_tcp_mss_unset(struct peer *peer)
5573 {
5574 UNSET_FLAG(peer->flags, PEER_FLAG_TCP_MSS);
5575 peer->tcp_mss = 0;
5576 }
5577
5578 /*
5579 * Helper function that is called after the name of the policy
5580 * being used by a peer has changed (AF specific). Automatically
5581 * initiates inbound or outbound processing as needed.
5582 */
5583 void peer_on_policy_change(struct peer *peer, afi_t afi, safi_t safi,
5584 int outbound)
5585 {
5586 if (outbound) {
5587 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5588 if (peer_established(peer))
5589 bgp_announce_route(peer, afi, safi, false);
5590 } else {
5591 if (!peer_established(peer))
5592 return;
5593
5594 if (bgp_soft_reconfig_in(peer, afi, safi))
5595 return;
5596
5597 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV) ||
5598 CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV)) {
5599 if (CHECK_FLAG(peer->af_cap[afi][safi],
5600 PEER_CAP_ORF_PREFIX_SM_ADV) &&
5601 (CHECK_FLAG(peer->af_cap[afi][safi],
5602 PEER_CAP_ORF_PREFIX_RM_RCV) ||
5603 CHECK_FLAG(peer->af_cap[afi][safi],
5604 PEER_CAP_ORF_PREFIX_RM_OLD_RCV)))
5605 peer_clear_soft(peer, afi, safi,
5606 BGP_CLEAR_SOFT_IN_ORF_PREFIX);
5607 else
5608 bgp_route_refresh_send(
5609 peer, afi, safi, 0, 0, 0,
5610 BGP_ROUTE_REFRESH_NORMAL);
5611 }
5612 }
5613 }
5614
5615
5616 /* neighbor weight. */
5617 int peer_weight_set(struct peer *peer, afi_t afi, safi_t safi, uint16_t weight)
5618 {
5619 struct peer *member;
5620 struct listnode *node, *nnode;
5621
5622 /* Set flag and configuration on peer. */
5623 peer_af_flag_set(peer, afi, safi, PEER_FLAG_WEIGHT);
5624 if (peer->weight[afi][safi] != weight) {
5625 peer->weight[afi][safi] = weight;
5626 peer_on_policy_change(peer, afi, safi, 0);
5627 }
5628
5629 /* Skip peer-group mechanics for regular peers. */
5630 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5631 return 0;
5632
5633 /*
5634 * Set flag and configuration on all peer-group members, unless they are
5635 * explicitly overriding peer-group configuration.
5636 */
5637 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5638 /* Skip peers with overridden configuration. */
5639 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5640 PEER_FLAG_WEIGHT))
5641 continue;
5642
5643 /* Set flag and configuration on peer-group member. */
5644 SET_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT);
5645 if (member->weight[afi][safi] != weight) {
5646 member->weight[afi][safi] = weight;
5647 peer_on_policy_change(member, afi, safi, 0);
5648 }
5649 }
5650
5651 return 0;
5652 }
5653
5654 int peer_weight_unset(struct peer *peer, afi_t afi, safi_t safi)
5655 {
5656 struct peer *member;
5657 struct listnode *node, *nnode;
5658
5659 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_WEIGHT))
5660 return 0;
5661
5662 /* Inherit configuration from peer-group if peer is member. */
5663 if (peer_group_active(peer)) {
5664 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_WEIGHT);
5665 PEER_ATTR_INHERIT(peer, peer->group, weight[afi][safi]);
5666
5667 peer_on_policy_change(peer, afi, safi, 0);
5668 return 0;
5669 }
5670
5671 /* Remove flag and configuration from peer. */
5672 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_WEIGHT);
5673 peer->weight[afi][safi] = 0;
5674 peer_on_policy_change(peer, afi, safi, 0);
5675
5676 /* Skip peer-group mechanics for regular peers. */
5677 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5678 return 0;
5679
5680 /*
5681 * Remove flag and configuration from all peer-group members, unless
5682 * they are explicitly overriding peer-group configuration.
5683 */
5684 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5685 /* Skip peers with overridden configuration. */
5686 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5687 PEER_FLAG_WEIGHT))
5688 continue;
5689
5690 /* Skip peers where flag is already disabled. */
5691 if (!CHECK_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT))
5692 continue;
5693
5694 /* Remove flag and configuration on peer-group member. */
5695 UNSET_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT);
5696 member->weight[afi][safi] = 0;
5697 peer_on_policy_change(member, afi, safi, 0);
5698 }
5699
5700 return 0;
5701 }
5702
5703 int peer_timers_set(struct peer *peer, uint32_t keepalive, uint32_t holdtime)
5704 {
5705 struct peer *member;
5706 struct listnode *node, *nnode;
5707
5708 if (keepalive > UINT16_MAX)
5709 return BGP_ERR_INVALID_VALUE;
5710
5711 if (holdtime > UINT16_MAX)
5712 return BGP_ERR_INVALID_VALUE;
5713
5714 if (holdtime < 3 && holdtime != 0)
5715 return BGP_ERR_INVALID_VALUE;
5716
5717 /* Set flag and configuration on peer. */
5718 peer_flag_set(peer, PEER_FLAG_TIMER);
5719 peer->holdtime = holdtime;
5720 peer->keepalive = (keepalive < holdtime / 3 ? keepalive : holdtime / 3);
5721
5722 /* Skip peer-group mechanics for regular peers. */
5723 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5724 return 0;
5725
5726 /*
5727 * Set flag and configuration on all peer-group members, unless they are
5728 * explicitly overriding peer-group configuration.
5729 */
5730 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5731 /* Skip peers with overridden configuration. */
5732 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER))
5733 continue;
5734
5735 /* Set flag and configuration on peer-group member. */
5736 SET_FLAG(member->flags, PEER_FLAG_TIMER);
5737 PEER_ATTR_INHERIT(member, peer->group, holdtime);
5738 PEER_ATTR_INHERIT(member, peer->group, keepalive);
5739 }
5740
5741 return 0;
5742 }
5743
5744 int peer_timers_unset(struct peer *peer)
5745 {
5746 struct peer *member;
5747 struct listnode *node, *nnode;
5748
5749 /* Inherit configuration from peer-group if peer is member. */
5750 if (peer_group_active(peer)) {
5751 peer_flag_inherit(peer, PEER_FLAG_TIMER);
5752 PEER_ATTR_INHERIT(peer, peer->group, holdtime);
5753 PEER_ATTR_INHERIT(peer, peer->group, keepalive);
5754 } else {
5755 /* Otherwise remove flag and configuration from peer. */
5756 peer_flag_unset(peer, PEER_FLAG_TIMER);
5757 peer->holdtime = 0;
5758 peer->keepalive = 0;
5759 }
5760
5761 /* Skip peer-group mechanics for regular peers. */
5762 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5763 return 0;
5764
5765 /*
5766 * Remove flag and configuration from all peer-group members, unless
5767 * they are explicitly overriding peer-group configuration.
5768 */
5769 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5770 /* Skip peers with overridden configuration. */
5771 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER))
5772 continue;
5773
5774 /* Remove flag and configuration on peer-group member. */
5775 UNSET_FLAG(member->flags, PEER_FLAG_TIMER);
5776 member->holdtime = 0;
5777 member->keepalive = 0;
5778 }
5779
5780 return 0;
5781 }
5782
5783 int peer_timers_connect_set(struct peer *peer, uint32_t connect)
5784 {
5785 struct peer *member;
5786 struct listnode *node, *nnode;
5787
5788 if (connect > UINT16_MAX)
5789 return BGP_ERR_INVALID_VALUE;
5790
5791 /* Set flag and configuration on peer. */
5792 peer_flag_set(peer, PEER_FLAG_TIMER_CONNECT);
5793 peer->connect = connect;
5794 peer->v_connect = connect;
5795
5796 /* Skip peer-group mechanics for regular peers. */
5797 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5798 if (!peer_established(peer)) {
5799 if (peer_active(peer))
5800 BGP_EVENT_ADD(peer, BGP_Stop);
5801 BGP_EVENT_ADD(peer, BGP_Start);
5802 }
5803 return 0;
5804 }
5805 /*
5806 * Set flag and configuration on all peer-group members, unless they are
5807 * explicitly overriding peer-group configuration.
5808 */
5809 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5810 /* Skip peers with overridden configuration. */
5811 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER_CONNECT))
5812 continue;
5813
5814 /* Set flag and configuration on peer-group member. */
5815 SET_FLAG(member->flags, PEER_FLAG_TIMER_CONNECT);
5816 member->connect = connect;
5817 member->v_connect = connect;
5818
5819 if (!peer_established(member)) {
5820 if (peer_active(member))
5821 BGP_EVENT_ADD(member, BGP_Stop);
5822 BGP_EVENT_ADD(member, BGP_Start);
5823 }
5824 }
5825
5826 return 0;
5827 }
5828
5829 int peer_timers_connect_unset(struct peer *peer)
5830 {
5831 struct peer *member;
5832 struct listnode *node, *nnode;
5833
5834 /* Inherit configuration from peer-group if peer is member. */
5835 if (peer_group_active(peer)) {
5836 peer_flag_inherit(peer, PEER_FLAG_TIMER_CONNECT);
5837 PEER_ATTR_INHERIT(peer, peer->group, connect);
5838 } else {
5839 /* Otherwise remove flag and configuration from peer. */
5840 peer_flag_unset(peer, PEER_FLAG_TIMER_CONNECT);
5841 peer->connect = 0;
5842 }
5843
5844 /* Set timer with fallback to default value. */
5845 if (peer->connect)
5846 peer->v_connect = peer->connect;
5847 else
5848 peer->v_connect = peer->bgp->default_connect_retry;
5849
5850 /* Skip peer-group mechanics for regular peers. */
5851 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5852 if (!peer_established(peer)) {
5853 if (peer_active(peer))
5854 BGP_EVENT_ADD(peer, BGP_Stop);
5855 BGP_EVENT_ADD(peer, BGP_Start);
5856 }
5857 return 0;
5858 }
5859 /*
5860 * Remove flag and configuration from all peer-group members, unless
5861 * they are explicitly overriding peer-group configuration.
5862 */
5863 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5864 /* Skip peers with overridden configuration. */
5865 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER_CONNECT))
5866 continue;
5867
5868 /* Remove flag and configuration on peer-group member. */
5869 UNSET_FLAG(member->flags, PEER_FLAG_TIMER_CONNECT);
5870 member->connect = 0;
5871 member->v_connect = peer->bgp->default_connect_retry;
5872
5873 if (!peer_established(member)) {
5874 if (peer_active(member))
5875 BGP_EVENT_ADD(member, BGP_Stop);
5876 BGP_EVENT_ADD(member, BGP_Start);
5877 }
5878 }
5879
5880 return 0;
5881 }
5882
5883 int peer_advertise_interval_set(struct peer *peer, uint32_t routeadv)
5884 {
5885 struct peer *member;
5886 struct listnode *node, *nnode;
5887
5888 if (routeadv > 600)
5889 return BGP_ERR_INVALID_VALUE;
5890
5891 /* Set flag and configuration on peer. */
5892 peer_flag_set(peer, PEER_FLAG_ROUTEADV);
5893 peer->routeadv = routeadv;
5894 peer->v_routeadv = routeadv;
5895
5896 /* Check if handling a regular peer. */
5897 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5898 /* Update peer route announcements. */
5899 update_group_adjust_peer_afs(peer);
5900 if (peer_established(peer))
5901 bgp_announce_route_all(peer);
5902
5903 /* Skip peer-group mechanics for regular peers. */
5904 return 0;
5905 }
5906
5907 /*
5908 * Set flag and configuration on all peer-group members, unless they are
5909 * explicitly overriding peer-group configuration.
5910 */
5911 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5912 /* Skip peers with overridden configuration. */
5913 if (CHECK_FLAG(member->flags_override, PEER_FLAG_ROUTEADV))
5914 continue;
5915
5916 /* Set flag and configuration on peer-group member. */
5917 SET_FLAG(member->flags, PEER_FLAG_ROUTEADV);
5918 member->routeadv = routeadv;
5919 member->v_routeadv = routeadv;
5920
5921 /* Update peer route announcements. */
5922 update_group_adjust_peer_afs(member);
5923 if (peer_established(member))
5924 bgp_announce_route_all(member);
5925 }
5926
5927 return 0;
5928 }
5929
5930 int peer_advertise_interval_unset(struct peer *peer)
5931 {
5932 struct peer *member;
5933 struct listnode *node, *nnode;
5934
5935 /* Inherit configuration from peer-group if peer is member. */
5936 if (peer_group_active(peer)) {
5937 peer_flag_inherit(peer, PEER_FLAG_ROUTEADV);
5938 PEER_ATTR_INHERIT(peer, peer->group, routeadv);
5939 } else {
5940 /* Otherwise remove flag and configuration from peer. */
5941 peer_flag_unset(peer, PEER_FLAG_ROUTEADV);
5942 peer->routeadv = 0;
5943 }
5944
5945 /* Set timer with fallback to default value. */
5946 if (peer->routeadv)
5947 peer->v_routeadv = peer->routeadv;
5948 else
5949 peer->v_routeadv = (peer->sort == BGP_PEER_IBGP)
5950 ? BGP_DEFAULT_IBGP_ROUTEADV
5951 : BGP_DEFAULT_EBGP_ROUTEADV;
5952
5953 /* Check if handling a regular peer. */
5954 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5955 /* Update peer route announcements. */
5956 update_group_adjust_peer_afs(peer);
5957 if (peer_established(peer))
5958 bgp_announce_route_all(peer);
5959
5960 /* Skip peer-group mechanics for regular peers. */
5961 return 0;
5962 }
5963
5964 /*
5965 * Remove flag and configuration from all peer-group members, unless
5966 * they are explicitly overriding peer-group configuration.
5967 */
5968 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5969 /* Skip peers with overridden configuration. */
5970 if (CHECK_FLAG(member->flags_override, PEER_FLAG_ROUTEADV))
5971 continue;
5972
5973 /* Remove flag and configuration on peer-group member. */
5974 UNSET_FLAG(member->flags, PEER_FLAG_ROUTEADV);
5975 member->routeadv = 0;
5976 member->v_routeadv = (member->sort == BGP_PEER_IBGP)
5977 ? BGP_DEFAULT_IBGP_ROUTEADV
5978 : BGP_DEFAULT_EBGP_ROUTEADV;
5979
5980 /* Update peer route announcements. */
5981 update_group_adjust_peer_afs(member);
5982 if (peer_established(member))
5983 bgp_announce_route_all(member);
5984 }
5985
5986 return 0;
5987 }
5988
5989 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5990 * interval
5991 */
5992 int peer_timers_delayopen_set(struct peer *peer, uint32_t delayopen)
5993 {
5994 struct peer *member;
5995 struct listnode *node;
5996
5997 /* Set peers session attribute flag and timer interval. */
5998 peer_flag_set(peer, PEER_FLAG_TIMER_DELAYOPEN);
5999 peer->delayopen = delayopen;
6000 peer->v_delayopen = delayopen;
6001
6002 /* Skip group mechanics for regular peers. */
6003 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6004 return 0;
6005
6006 /* Set flag and configuration on all peer-group members, unless they are
6007 * explicitly overriding peer-group configuration.
6008 */
6009 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
6010 /* Skip peers with overridden configuration. */
6011 if (CHECK_FLAG(member->flags_override,
6012 PEER_FLAG_TIMER_DELAYOPEN))
6013 continue;
6014
6015 /* Set session attribute flag and timer intervals on peer-group
6016 * member.
6017 */
6018 SET_FLAG(member->flags, PEER_FLAG_TIMER_DELAYOPEN);
6019 member->delayopen = delayopen;
6020 member->v_delayopen = delayopen;
6021 }
6022
6023 return 0;
6024 }
6025
6026 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6027 * DelayOpenTimer interval to the default value.
6028 */
6029 int peer_timers_delayopen_unset(struct peer *peer)
6030 {
6031 struct peer *member;
6032 struct listnode *node;
6033
6034 /* Inherit configuration from peer-group if peer is member. */
6035 if (peer_group_active(peer)) {
6036 peer_flag_inherit(peer, PEER_FLAG_TIMER_DELAYOPEN);
6037 PEER_ATTR_INHERIT(peer, peer->group, delayopen);
6038 } else {
6039 /* Otherwise remove session attribute flag and set timer
6040 * interval to default value.
6041 */
6042 peer_flag_unset(peer, PEER_FLAG_TIMER_DELAYOPEN);
6043 peer->delayopen = peer->bgp->default_delayopen;
6044 }
6045
6046 /* Set timer value to zero */
6047 peer->v_delayopen = 0;
6048
6049 /* Skip peer-group mechanics for regular peers. */
6050 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6051 return 0;
6052
6053 /* Remove flag and configuration from all peer-group members, unless
6054 * they are explicitly overriding peer-group configuration.
6055 */
6056 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
6057 /* Skip peers with overridden configuration. */
6058 if (CHECK_FLAG(member->flags_override,
6059 PEER_FLAG_TIMER_DELAYOPEN))
6060 continue;
6061
6062 /* Remove session attribute flag, reset the timer interval to
6063 * the default value and set the timer value to zero.
6064 */
6065 UNSET_FLAG(member->flags, PEER_FLAG_TIMER_DELAYOPEN);
6066 member->delayopen = peer->bgp->default_delayopen;
6067 member->v_delayopen = 0;
6068 }
6069
6070 return 0;
6071 }
6072
6073 /* neighbor interface */
6074 void peer_interface_set(struct peer *peer, const char *str)
6075 {
6076 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
6077 peer->ifname = XSTRDUP(MTYPE_BGP_PEER_IFNAME, str);
6078 }
6079
6080 void peer_interface_unset(struct peer *peer)
6081 {
6082 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
6083 }
6084
6085 /* Allow-as in. */
6086 int peer_allowas_in_set(struct peer *peer, afi_t afi, safi_t safi,
6087 int allow_num, int origin)
6088 {
6089 struct peer *member;
6090 struct listnode *node, *nnode;
6091
6092 if (!origin && (allow_num < 1 || allow_num > 10))
6093 return BGP_ERR_INVALID_VALUE;
6094
6095 /* Set flag and configuration on peer. */
6096 peer_af_flag_set(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
6097 if (origin) {
6098 if (peer->allowas_in[afi][safi] != 0
6099 || !CHECK_FLAG(peer->af_flags[afi][safi],
6100 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6101 peer_af_flag_set(peer, afi, safi,
6102 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6103 peer->allowas_in[afi][safi] = 0;
6104 peer_on_policy_change(peer, afi, safi, 0);
6105 }
6106 } else {
6107 if (peer->allowas_in[afi][safi] != allow_num
6108 || CHECK_FLAG(peer->af_flags[afi][safi],
6109 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6110
6111 peer_af_flag_unset(peer, afi, safi,
6112 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6113 peer->allowas_in[afi][safi] = allow_num;
6114 peer_on_policy_change(peer, afi, safi, 0);
6115 }
6116 }
6117
6118 /* Skip peer-group mechanics for regular peers. */
6119 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6120 return 0;
6121
6122 /*
6123 * Set flag and configuration on all peer-group members, unless
6124 * they are explicitly overriding peer-group configuration.
6125 */
6126 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6127 /* Skip peers with overridden configuration. */
6128 if (CHECK_FLAG(member->af_flags_override[afi][safi],
6129 PEER_FLAG_ALLOWAS_IN))
6130 continue;
6131
6132 /* Set flag and configuration on peer-group member. */
6133 SET_FLAG(member->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
6134 if (origin) {
6135 if (member->allowas_in[afi][safi] != 0
6136 || !CHECK_FLAG(member->af_flags[afi][safi],
6137 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6138 SET_FLAG(member->af_flags[afi][safi],
6139 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6140 member->allowas_in[afi][safi] = 0;
6141 peer_on_policy_change(peer, afi, safi, 0);
6142 }
6143 } else {
6144 if (member->allowas_in[afi][safi] != allow_num
6145 || CHECK_FLAG(member->af_flags[afi][safi],
6146 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6147 UNSET_FLAG(member->af_flags[afi][safi],
6148 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6149 member->allowas_in[afi][safi] = allow_num;
6150 peer_on_policy_change(peer, afi, safi, 0);
6151 }
6152 }
6153 }
6154
6155 return 0;
6156 }
6157
6158 int peer_allowas_in_unset(struct peer *peer, afi_t afi, safi_t safi)
6159 {
6160 struct peer *member;
6161 struct listnode *node, *nnode;
6162
6163 /* Skip peer if flag is already disabled. */
6164 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
6165 return 0;
6166
6167 /* Inherit configuration from peer-group if peer is member. */
6168 if (peer_group_active(peer)) {
6169 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
6170 peer_af_flag_inherit(peer, afi, safi,
6171 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6172 PEER_ATTR_INHERIT(peer, peer->group, allowas_in[afi][safi]);
6173 peer_on_policy_change(peer, afi, safi, 0);
6174
6175 return 0;
6176 }
6177
6178 /* Remove flag and configuration from peer. */
6179 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
6180 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_ALLOWAS_IN_ORIGIN);
6181 peer->allowas_in[afi][safi] = 0;
6182 peer_on_policy_change(peer, afi, safi, 0);
6183
6184 /* Skip peer-group mechanics if handling a regular peer. */
6185 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6186 return 0;
6187
6188 /*
6189 * Remove flags and configuration from all peer-group members, unless
6190 * they are explicitly overriding peer-group configuration.
6191 */
6192 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6193 /* Skip peers with overridden configuration. */
6194 if (CHECK_FLAG(member->af_flags_override[afi][safi],
6195 PEER_FLAG_ALLOWAS_IN))
6196 continue;
6197
6198 /* Remove flags and configuration on peer-group member. */
6199 UNSET_FLAG(member->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
6200 UNSET_FLAG(member->af_flags[afi][safi],
6201 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6202 member->allowas_in[afi][safi] = 0;
6203 peer_on_policy_change(member, afi, safi, 0);
6204 }
6205
6206 return 0;
6207 }
6208
6209 int peer_local_as_set(struct peer *peer, as_t as, bool no_prepend,
6210 bool replace_as)
6211 {
6212 bool old_no_prepend, old_replace_as;
6213 struct bgp *bgp = peer->bgp;
6214 struct peer *member;
6215 struct listnode *node, *nnode;
6216
6217 if (bgp->as == as)
6218 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS;
6219
6220 /* Save previous flag states. */
6221 old_no_prepend =
6222 !!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6223 old_replace_as =
6224 !!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6225
6226 /* Set flag and configuration on peer. */
6227 peer_flag_set(peer, PEER_FLAG_LOCAL_AS);
6228 peer_flag_modify(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND, no_prepend);
6229 peer_flag_modify(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS, replace_as);
6230
6231 if (peer->change_local_as == as && old_no_prepend == no_prepend
6232 && old_replace_as == replace_as)
6233 return 0;
6234 peer->change_local_as = as;
6235 (void)peer_sort(peer);
6236
6237 /* Check if handling a regular peer. */
6238 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6239 return 0;
6240
6241 /*
6242 * Set flag and configuration on all peer-group members, unless they are
6243 * explicitly overriding peer-group configuration.
6244 */
6245 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6246 /* Skip peers with overridden configuration. */
6247 if (CHECK_FLAG(member->flags_override, PEER_FLAG_LOCAL_AS))
6248 continue;
6249
6250 /* Skip peers with the same configuration. */
6251 old_no_prepend = CHECK_FLAG(member->flags,
6252 PEER_FLAG_LOCAL_AS_NO_PREPEND);
6253 old_replace_as = CHECK_FLAG(member->flags,
6254 PEER_FLAG_LOCAL_AS_REPLACE_AS);
6255 if (member->change_local_as == as
6256 && CHECK_FLAG(member->flags, PEER_FLAG_LOCAL_AS)
6257 && old_no_prepend == no_prepend
6258 && old_replace_as == replace_as)
6259 continue;
6260
6261 /* Set flag and configuration on peer-group member. */
6262 SET_FLAG(member->flags, PEER_FLAG_LOCAL_AS);
6263 COND_FLAG(member->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND,
6264 no_prepend);
6265 COND_FLAG(member->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS,
6266 replace_as);
6267 member->change_local_as = as;
6268 }
6269
6270 return 0;
6271 }
6272
6273 int peer_local_as_unset(struct peer *peer)
6274 {
6275 struct peer *member;
6276 struct listnode *node, *nnode;
6277
6278 if (!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS))
6279 return 0;
6280
6281 /* Inherit configuration from peer-group if peer is member. */
6282 if (peer_group_active(peer)) {
6283 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS);
6284 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6285 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6286 PEER_ATTR_INHERIT(peer, peer->group, change_local_as);
6287 } else {
6288 /* Otherwise remove flag and configuration from peer. */
6289 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS);
6290 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6291 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6292 peer->change_local_as = 0;
6293 }
6294
6295 /* Check if handling a regular peer. */
6296 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6297 /* Send notification or stop peer depending on state. */
6298 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
6299 peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6300 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6301 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6302 } else
6303 BGP_EVENT_ADD(peer, BGP_Stop);
6304
6305 /* Skip peer-group mechanics for regular peers. */
6306 return 0;
6307 }
6308
6309 /*
6310 * Remove flag and configuration from all peer-group members, unless
6311 * they are explicitly overriding peer-group configuration.
6312 */
6313 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6314 /* Skip peers with overridden configuration. */
6315 if (CHECK_FLAG(member->flags_override, PEER_FLAG_LOCAL_AS))
6316 continue;
6317
6318 /* Remove flag and configuration on peer-group member. */
6319 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS);
6320 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6321 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6322 member->change_local_as = 0;
6323
6324 /* Send notification or stop peer depending on state. */
6325 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
6326 member->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6327 bgp_notify_send(member, BGP_NOTIFY_CEASE,
6328 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6329 } else
6330 bgp_session_reset(member);
6331 }
6332
6333 return 0;
6334 }
6335
6336 /* Set password for authenticating with the peer. */
6337 int peer_password_set(struct peer *peer, const char *password)
6338 {
6339 struct peer *member;
6340 struct listnode *node, *nnode;
6341 int len = password ? strlen(password) : 0;
6342 int ret = BGP_SUCCESS;
6343
6344 if ((len < PEER_PASSWORD_MINLEN) || (len > PEER_PASSWORD_MAXLEN))
6345 return BGP_ERR_INVALID_VALUE;
6346
6347 /* Set flag and configuration on peer. */
6348 peer_flag_set(peer, PEER_FLAG_PASSWORD);
6349 if (peer->password && strcmp(peer->password, password) == 0)
6350 return 0;
6351 XFREE(MTYPE_PEER_PASSWORD, peer->password);
6352 peer->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
6353
6354 /* Check if handling a regular peer. */
6355 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6356 /* Send notification or reset peer depending on state. */
6357 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
6358 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6359 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6360 else
6361 bgp_session_reset(peer);
6362
6363 /*
6364 * Attempt to install password on socket and skip peer-group
6365 * mechanics.
6366 */
6367 if (BGP_PEER_SU_UNSPEC(peer))
6368 return BGP_SUCCESS;
6369 return (bgp_md5_set(peer) >= 0) ? BGP_SUCCESS
6370 : BGP_ERR_TCPSIG_FAILED;
6371 }
6372
6373 /*
6374 * Set flag and configuration on all peer-group members, unless they are
6375 * explicitly overriding peer-group configuration.
6376 */
6377 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6378 /* Skip peers with overridden configuration. */
6379 if (CHECK_FLAG(member->flags_override, PEER_FLAG_PASSWORD))
6380 continue;
6381
6382 /* Skip peers with the same password. */
6383 if (member->password && strcmp(member->password, password) == 0)
6384 continue;
6385
6386 /* Set flag and configuration on peer-group member. */
6387 SET_FLAG(member->flags, PEER_FLAG_PASSWORD);
6388 if (member->password)
6389 XFREE(MTYPE_PEER_PASSWORD, member->password);
6390 member->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
6391
6392 /* Send notification or reset peer depending on state. */
6393 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status))
6394 bgp_notify_send(member, BGP_NOTIFY_CEASE,
6395 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6396 else
6397 bgp_session_reset(member);
6398
6399 /* Attempt to install password on socket. */
6400 if (!BGP_PEER_SU_UNSPEC(member) && bgp_md5_set(member) < 0)
6401 ret = BGP_ERR_TCPSIG_FAILED;
6402 }
6403
6404 /* Set flag and configuration on all peer-group listen ranges */
6405 struct listnode *ln;
6406 struct prefix *lr;
6407
6408 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP], ln, lr))
6409 bgp_md5_set_prefix(peer->bgp, lr, password);
6410 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP6], ln, lr))
6411 bgp_md5_set_prefix(peer->bgp, lr, password);
6412
6413 return ret;
6414 }
6415
6416 int peer_password_unset(struct peer *peer)
6417 {
6418 struct peer *member;
6419 struct listnode *node, *nnode;
6420
6421 if (!CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD))
6422 return 0;
6423
6424 /* Inherit configuration from peer-group if peer is member. */
6425 if (peer_group_active(peer)) {
6426 peer_flag_inherit(peer, PEER_FLAG_PASSWORD);
6427 PEER_STR_ATTR_INHERIT(peer, peer->group, password,
6428 MTYPE_PEER_PASSWORD);
6429 } else {
6430 /* Otherwise remove flag and configuration from peer. */
6431 peer_flag_unset(peer, PEER_FLAG_PASSWORD);
6432 XFREE(MTYPE_PEER_PASSWORD, peer->password);
6433 }
6434
6435 /* Check if handling a regular peer. */
6436 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6437 /* Send notification or reset peer depending on state. */
6438 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
6439 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6440 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6441 else
6442 bgp_session_reset(peer);
6443
6444 /* Attempt to uninstall password on socket. */
6445 if (!BGP_PEER_SU_UNSPEC(peer))
6446 bgp_md5_unset(peer);
6447 /* Skip peer-group mechanics for regular peers. */
6448 return 0;
6449 }
6450
6451 /*
6452 * Remove flag and configuration from all peer-group members, unless
6453 * they are explicitly overriding peer-group configuration.
6454 */
6455 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6456 /* Skip peers with overridden configuration. */
6457 if (CHECK_FLAG(member->flags_override, PEER_FLAG_PASSWORD))
6458 continue;
6459
6460 /* Remove flag and configuration on peer-group member. */
6461 UNSET_FLAG(member->flags, PEER_FLAG_PASSWORD);
6462 XFREE(MTYPE_PEER_PASSWORD, member->password);
6463
6464 /* Send notification or reset peer depending on state. */
6465 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status))
6466 bgp_notify_send(member, BGP_NOTIFY_CEASE,
6467 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6468 else
6469 bgp_session_reset(member);
6470
6471 /* Attempt to uninstall password on socket. */
6472 if (!BGP_PEER_SU_UNSPEC(member))
6473 bgp_md5_unset(member);
6474 }
6475
6476 /* Set flag and configuration on all peer-group listen ranges */
6477 struct listnode *ln;
6478 struct prefix *lr;
6479
6480 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP], ln, lr))
6481 bgp_md5_unset_prefix(peer->bgp, lr);
6482 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP6], ln, lr))
6483 bgp_md5_unset_prefix(peer->bgp, lr);
6484
6485 return 0;
6486 }
6487
6488
6489 /* Set distribute list to the peer. */
6490 int peer_distribute_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6491 const char *name)
6492 {
6493 struct peer *member;
6494 struct bgp_filter *filter;
6495 struct listnode *node, *nnode;
6496
6497 if (direct != FILTER_IN && direct != FILTER_OUT)
6498 return BGP_ERR_INVALID_VALUE;
6499
6500 /* Set configuration on peer. */
6501 filter = &peer->filter[afi][safi];
6502 if (filter->plist[direct].name)
6503 return BGP_ERR_PEER_FILTER_CONFLICT;
6504 if (filter->dlist[direct].name)
6505 XFREE(MTYPE_BGP_FILTER_NAME, filter->dlist[direct].name);
6506 filter->dlist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6507 filter->dlist[direct].alist = access_list_lookup(afi, name);
6508
6509 /* Check if handling a regular peer. */
6510 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6511 /* Set override-flag and process peer route updates. */
6512 SET_FLAG(peer->filter_override[afi][safi][direct],
6513 PEER_FT_DISTRIBUTE_LIST);
6514 peer_on_policy_change(peer, afi, safi,
6515 (direct == FILTER_OUT) ? 1 : 0);
6516
6517 /* Skip peer-group mechanics for regular peers. */
6518 return 0;
6519 }
6520
6521 /*
6522 * Set configuration on all peer-group members, un less they are
6523 * explicitly overriding peer-group configuration.
6524 */
6525 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6526 /* Skip peers with overridden configuration. */
6527 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6528 PEER_FT_DISTRIBUTE_LIST))
6529 continue;
6530
6531 /* Set configuration on peer-group member. */
6532 filter = &member->filter[afi][safi];
6533 if (filter->dlist[direct].name)
6534 XFREE(MTYPE_BGP_FILTER_NAME,
6535 filter->dlist[direct].name);
6536 filter->dlist[direct].name =
6537 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6538 filter->dlist[direct].alist = access_list_lookup(afi, name);
6539
6540 /* Process peer route updates. */
6541 peer_on_policy_change(member, afi, safi,
6542 (direct == FILTER_OUT) ? 1 : 0);
6543 }
6544
6545 return 0;
6546 }
6547
6548 int peer_distribute_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6549 {
6550 struct peer *member;
6551 struct bgp_filter *filter;
6552 struct listnode *node, *nnode;
6553
6554 if (direct != FILTER_IN && direct != FILTER_OUT)
6555 return BGP_ERR_INVALID_VALUE;
6556
6557 /* Unset override-flag unconditionally. */
6558 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6559 PEER_FT_DISTRIBUTE_LIST);
6560
6561 /* Inherit configuration from peer-group if peer is member. */
6562 if (peer_group_active(peer)) {
6563 PEER_STR_ATTR_INHERIT(peer, peer->group,
6564 filter[afi][safi].dlist[direct].name,
6565 MTYPE_BGP_FILTER_NAME);
6566 PEER_ATTR_INHERIT(peer, peer->group,
6567 filter[afi][safi].dlist[direct].alist);
6568 } else {
6569 /* Otherwise remove configuration from peer. */
6570 filter = &peer->filter[afi][safi];
6571 if (filter->dlist[direct].name)
6572 XFREE(MTYPE_BGP_FILTER_NAME,
6573 filter->dlist[direct].name);
6574 filter->dlist[direct].name = NULL;
6575 filter->dlist[direct].alist = NULL;
6576 }
6577
6578 /* Check if handling a regular peer. */
6579 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6580 /* Process peer route updates. */
6581 peer_on_policy_change(peer, afi, safi,
6582 (direct == FILTER_OUT) ? 1 : 0);
6583
6584 /* Skip peer-group mechanics for regular peers. */
6585 return 0;
6586 }
6587
6588 /*
6589 * Remove configuration on all peer-group members, unless they are
6590 * explicitly overriding peer-group configuration.
6591 */
6592 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6593 /* Skip peers with overridden configuration. */
6594 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6595 PEER_FT_DISTRIBUTE_LIST))
6596 continue;
6597
6598 /* Remove configuration on peer-group member. */
6599 filter = &member->filter[afi][safi];
6600 if (filter->dlist[direct].name)
6601 XFREE(MTYPE_BGP_FILTER_NAME,
6602 filter->dlist[direct].name);
6603 filter->dlist[direct].name = NULL;
6604 filter->dlist[direct].alist = NULL;
6605
6606 /* Process peer route updates. */
6607 peer_on_policy_change(member, afi, safi,
6608 (direct == FILTER_OUT) ? 1 : 0);
6609 }
6610
6611 return 0;
6612 }
6613
6614 /* Update distribute list. */
6615 static void peer_distribute_update(struct access_list *access)
6616 {
6617 afi_t afi;
6618 safi_t safi;
6619 int direct;
6620 struct listnode *mnode, *mnnode;
6621 struct listnode *node, *nnode;
6622 struct bgp *bgp;
6623 struct peer *peer;
6624 struct peer_group *group;
6625 struct bgp_filter *filter;
6626
6627 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6628 if (access->name)
6629 update_group_policy_update(bgp,
6630 BGP_POLICY_DISTRIBUTE_LIST,
6631 access->name, true, 0);
6632 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
6633 FOREACH_AFI_SAFI (afi, safi) {
6634 filter = &peer->filter[afi][safi];
6635
6636 for (direct = FILTER_IN; direct < FILTER_MAX;
6637 direct++) {
6638 if (filter->dlist[direct].name)
6639 filter->dlist[direct]
6640 .alist = access_list_lookup(
6641 afi,
6642 filter->dlist[direct]
6643 .name);
6644 else
6645 filter->dlist[direct].alist =
6646 NULL;
6647 }
6648 }
6649 }
6650 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
6651 FOREACH_AFI_SAFI (afi, safi) {
6652 filter = &group->conf->filter[afi][safi];
6653
6654 for (direct = FILTER_IN; direct < FILTER_MAX;
6655 direct++) {
6656 if (filter->dlist[direct].name)
6657 filter->dlist[direct]
6658 .alist = access_list_lookup(
6659 afi,
6660 filter->dlist[direct]
6661 .name);
6662 else
6663 filter->dlist[direct].alist =
6664 NULL;
6665 }
6666 }
6667 }
6668 #ifdef ENABLE_BGP_VNC
6669 vnc_prefix_list_update(bgp);
6670 #endif
6671 }
6672 }
6673
6674 /* Set prefix list to the peer. */
6675 int peer_prefix_list_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6676 const char *name)
6677 {
6678 struct peer *member;
6679 struct bgp_filter *filter;
6680 struct listnode *node, *nnode;
6681
6682 if (direct != FILTER_IN && direct != FILTER_OUT)
6683 return BGP_ERR_INVALID_VALUE;
6684
6685 /* Set configuration on peer. */
6686 filter = &peer->filter[afi][safi];
6687 if (filter->dlist[direct].name)
6688 return BGP_ERR_PEER_FILTER_CONFLICT;
6689 if (filter->plist[direct].name)
6690 XFREE(MTYPE_BGP_FILTER_NAME, filter->plist[direct].name);
6691 filter->plist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6692 filter->plist[direct].plist = prefix_list_lookup(afi, name);
6693
6694 /* Check if handling a regular peer. */
6695 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6696 /* Set override-flag and process peer route updates. */
6697 SET_FLAG(peer->filter_override[afi][safi][direct],
6698 PEER_FT_PREFIX_LIST);
6699 peer_on_policy_change(peer, afi, safi,
6700 (direct == FILTER_OUT) ? 1 : 0);
6701
6702 /* Skip peer-group mechanics for regular peers. */
6703 return 0;
6704 }
6705
6706 /*
6707 * Set configuration on all peer-group members, unless they are
6708 * explicitly overriding peer-group configuration.
6709 */
6710 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6711 /* Skip peers with overridden configuration. */
6712 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6713 PEER_FT_PREFIX_LIST))
6714 continue;
6715
6716 /* Set configuration on peer-group member. */
6717 filter = &member->filter[afi][safi];
6718 if (filter->plist[direct].name)
6719 XFREE(MTYPE_BGP_FILTER_NAME,
6720 filter->plist[direct].name);
6721 filter->plist[direct].name =
6722 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6723 filter->plist[direct].plist = prefix_list_lookup(afi, name);
6724
6725 /* Process peer route updates. */
6726 peer_on_policy_change(member, afi, safi,
6727 (direct == FILTER_OUT) ? 1 : 0);
6728 }
6729
6730 return 0;
6731 }
6732
6733 int peer_prefix_list_unset(struct peer *peer, afi_t afi, safi_t safi,
6734 int direct)
6735 {
6736 struct peer *member;
6737 struct bgp_filter *filter;
6738 struct listnode *node, *nnode;
6739
6740 if (direct != FILTER_IN && direct != FILTER_OUT)
6741 return BGP_ERR_INVALID_VALUE;
6742
6743 /* Unset override-flag unconditionally. */
6744 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6745 PEER_FT_PREFIX_LIST);
6746
6747 /* Inherit configuration from peer-group if peer is member. */
6748 if (peer_group_active(peer)) {
6749 PEER_STR_ATTR_INHERIT(peer, peer->group,
6750 filter[afi][safi].plist[direct].name,
6751 MTYPE_BGP_FILTER_NAME);
6752 PEER_ATTR_INHERIT(peer, peer->group,
6753 filter[afi][safi].plist[direct].plist);
6754 } else {
6755 /* Otherwise remove configuration from peer. */
6756 filter = &peer->filter[afi][safi];
6757 if (filter->plist[direct].name)
6758 XFREE(MTYPE_BGP_FILTER_NAME,
6759 filter->plist[direct].name);
6760 filter->plist[direct].name = NULL;
6761 filter->plist[direct].plist = NULL;
6762 }
6763
6764 /* Check if handling a regular peer. */
6765 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6766 /* Process peer route updates. */
6767 peer_on_policy_change(peer, afi, safi,
6768 (direct == FILTER_OUT) ? 1 : 0);
6769
6770 /* Skip peer-group mechanics for regular peers. */
6771 return 0;
6772 }
6773
6774 /*
6775 * Remove configuration on all peer-group members, unless they are
6776 * explicitly overriding peer-group configuration.
6777 */
6778 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6779 /* Skip peers with overridden configuration. */
6780 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6781 PEER_FT_PREFIX_LIST))
6782 continue;
6783
6784 /* Remove configuration on peer-group member. */
6785 filter = &member->filter[afi][safi];
6786 if (filter->plist[direct].name)
6787 XFREE(MTYPE_BGP_FILTER_NAME,
6788 filter->plist[direct].name);
6789 filter->plist[direct].name = NULL;
6790 filter->plist[direct].plist = NULL;
6791
6792 /* Process peer route updates. */
6793 peer_on_policy_change(member, afi, safi,
6794 (direct == FILTER_OUT) ? 1 : 0);
6795 }
6796
6797 return 0;
6798 }
6799
6800 /* Update prefix-list list. */
6801 static void peer_prefix_list_update(struct prefix_list *plist)
6802 {
6803 struct listnode *mnode, *mnnode;
6804 struct listnode *node, *nnode;
6805 struct bgp *bgp;
6806 struct peer *peer;
6807 struct peer_group *group;
6808 struct bgp_filter *filter;
6809 afi_t afi;
6810 safi_t safi;
6811 int direct;
6812
6813 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6814
6815 /*
6816 * Update the prefix-list on update groups.
6817 */
6818 update_group_policy_update(
6819 bgp, BGP_POLICY_PREFIX_LIST,
6820 plist ? prefix_list_name(plist) : NULL, true, 0);
6821
6822 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
6823 FOREACH_AFI_SAFI (afi, safi) {
6824 filter = &peer->filter[afi][safi];
6825
6826 for (direct = FILTER_IN; direct < FILTER_MAX;
6827 direct++) {
6828 if (filter->plist[direct].name)
6829 filter->plist[direct]
6830 .plist = prefix_list_lookup(
6831 afi,
6832 filter->plist[direct]
6833 .name);
6834 else
6835 filter->plist[direct].plist =
6836 NULL;
6837 }
6838
6839 /* If we touch prefix-list, we need to process
6840 * new updates. This is important for ORF to
6841 * work correctly as well.
6842 */
6843 if (peer->afc_nego[afi][safi])
6844 peer_on_policy_change(peer, afi, safi,
6845 0);
6846 }
6847 }
6848 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
6849 FOREACH_AFI_SAFI (afi, safi) {
6850 filter = &group->conf->filter[afi][safi];
6851
6852 for (direct = FILTER_IN; direct < FILTER_MAX;
6853 direct++) {
6854 if (filter->plist[direct].name)
6855 filter->plist[direct]
6856 .plist = prefix_list_lookup(
6857 afi,
6858 filter->plist[direct]
6859 .name);
6860 else
6861 filter->plist[direct].plist =
6862 NULL;
6863 }
6864 }
6865 }
6866 }
6867 }
6868
6869 int peer_aslist_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6870 const char *name)
6871 {
6872 struct peer *member;
6873 struct bgp_filter *filter;
6874 struct listnode *node, *nnode;
6875
6876 if (direct != FILTER_IN && direct != FILTER_OUT)
6877 return BGP_ERR_INVALID_VALUE;
6878
6879 /* Set configuration on peer. */
6880 filter = &peer->filter[afi][safi];
6881 if (filter->aslist[direct].name)
6882 XFREE(MTYPE_BGP_FILTER_NAME, filter->aslist[direct].name);
6883 filter->aslist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6884 filter->aslist[direct].aslist = as_list_lookup(name);
6885
6886 /* Check if handling a regular peer. */
6887 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6888 /* Set override-flag and process peer route updates. */
6889 SET_FLAG(peer->filter_override[afi][safi][direct],
6890 PEER_FT_FILTER_LIST);
6891 peer_on_policy_change(peer, afi, safi,
6892 (direct == FILTER_OUT) ? 1 : 0);
6893
6894 /* Skip peer-group mechanics for regular peers. */
6895 return 0;
6896 }
6897
6898 /*
6899 * Set configuration on all peer-group members, unless they are
6900 * explicitly overriding peer-group configuration.
6901 */
6902 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6903 /* Skip peers with overridden configuration. */
6904 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6905 PEER_FT_FILTER_LIST))
6906 continue;
6907
6908 /* Set configuration on peer-group member. */
6909 filter = &member->filter[afi][safi];
6910 if (filter->aslist[direct].name)
6911 XFREE(MTYPE_BGP_FILTER_NAME,
6912 filter->aslist[direct].name);
6913 filter->aslist[direct].name =
6914 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6915 filter->aslist[direct].aslist = as_list_lookup(name);
6916
6917 /* Process peer route updates. */
6918 peer_on_policy_change(member, afi, safi,
6919 (direct == FILTER_OUT) ? 1 : 0);
6920 }
6921
6922 return 0;
6923 }
6924
6925 int peer_aslist_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6926 {
6927 struct peer *member;
6928 struct bgp_filter *filter;
6929 struct listnode *node, *nnode;
6930
6931 if (direct != FILTER_IN && direct != FILTER_OUT)
6932 return BGP_ERR_INVALID_VALUE;
6933
6934 /* Unset override-flag unconditionally. */
6935 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6936 PEER_FT_FILTER_LIST);
6937
6938 /* Inherit configuration from peer-group if peer is member. */
6939 if (peer_group_active(peer)) {
6940 PEER_STR_ATTR_INHERIT(peer, peer->group,
6941 filter[afi][safi].aslist[direct].name,
6942 MTYPE_BGP_FILTER_NAME);
6943 PEER_ATTR_INHERIT(peer, peer->group,
6944 filter[afi][safi].aslist[direct].aslist);
6945 } else {
6946 /* Otherwise remove configuration from peer. */
6947 filter = &peer->filter[afi][safi];
6948 if (filter->aslist[direct].name)
6949 XFREE(MTYPE_BGP_FILTER_NAME,
6950 filter->aslist[direct].name);
6951 filter->aslist[direct].name = NULL;
6952 filter->aslist[direct].aslist = NULL;
6953 }
6954
6955 /* Check if handling a regular peer. */
6956 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6957 /* Process peer route updates. */
6958 peer_on_policy_change(peer, afi, safi,
6959 (direct == FILTER_OUT) ? 1 : 0);
6960
6961 /* Skip peer-group mechanics for regular peers. */
6962 return 0;
6963 }
6964
6965 /*
6966 * Remove configuration on all peer-group members, unless they are
6967 * explicitly overriding peer-group configuration.
6968 */
6969 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6970 /* Skip peers with overridden configuration. */
6971 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6972 PEER_FT_FILTER_LIST))
6973 continue;
6974
6975 /* Remove configuration on peer-group member. */
6976 filter = &member->filter[afi][safi];
6977 if (filter->aslist[direct].name)
6978 XFREE(MTYPE_BGP_FILTER_NAME,
6979 filter->aslist[direct].name);
6980 filter->aslist[direct].name = NULL;
6981 filter->aslist[direct].aslist = NULL;
6982
6983 /* Process peer route updates. */
6984 peer_on_policy_change(member, afi, safi,
6985 (direct == FILTER_OUT) ? 1 : 0);
6986 }
6987
6988 return 0;
6989 }
6990
6991 static void peer_aslist_update(const char *aslist_name)
6992 {
6993 afi_t afi;
6994 safi_t safi;
6995 int direct;
6996 struct listnode *mnode, *mnnode;
6997 struct listnode *node, *nnode;
6998 struct bgp *bgp;
6999 struct peer *peer;
7000 struct peer_group *group;
7001 struct bgp_filter *filter;
7002
7003 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
7004 update_group_policy_update(bgp, BGP_POLICY_FILTER_LIST,
7005 aslist_name, true, 0);
7006
7007 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
7008 FOREACH_AFI_SAFI (afi, safi) {
7009 filter = &peer->filter[afi][safi];
7010
7011 for (direct = FILTER_IN; direct < FILTER_MAX;
7012 direct++) {
7013 if (filter->aslist[direct].name)
7014 filter->aslist[direct]
7015 .aslist = as_list_lookup(
7016 filter->aslist[direct]
7017 .name);
7018 else
7019 filter->aslist[direct].aslist =
7020 NULL;
7021 }
7022 }
7023 }
7024 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
7025 FOREACH_AFI_SAFI (afi, safi) {
7026 filter = &group->conf->filter[afi][safi];
7027
7028 for (direct = FILTER_IN; direct < FILTER_MAX;
7029 direct++) {
7030 if (filter->aslist[direct].name)
7031 filter->aslist[direct]
7032 .aslist = as_list_lookup(
7033 filter->aslist[direct]
7034 .name);
7035 else
7036 filter->aslist[direct].aslist =
7037 NULL;
7038 }
7039 }
7040 }
7041 }
7042 }
7043
7044 static void peer_aslist_add(char *aslist_name)
7045 {
7046 peer_aslist_update(aslist_name);
7047 route_map_notify_dependencies(aslist_name, RMAP_EVENT_ASLIST_ADDED);
7048 }
7049
7050 static void peer_aslist_del(const char *aslist_name)
7051 {
7052 peer_aslist_update(aslist_name);
7053 route_map_notify_dependencies(aslist_name, RMAP_EVENT_ASLIST_DELETED);
7054 }
7055
7056
7057 int peer_route_map_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
7058 const char *name, struct route_map *route_map)
7059 {
7060 struct peer *member;
7061 struct bgp_filter *filter;
7062 struct listnode *node, *nnode;
7063
7064 if (direct != RMAP_IN && direct != RMAP_OUT)
7065 return BGP_ERR_INVALID_VALUE;
7066
7067 /* Set configuration on peer. */
7068 filter = &peer->filter[afi][safi];
7069 if (filter->map[direct].name) {
7070 /* If the neighbor is configured with the same route-map
7071 * again then, ignore the duplicate configuration.
7072 */
7073 if (strcmp(filter->map[direct].name, name) == 0)
7074 return 0;
7075
7076 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7077 }
7078 route_map_counter_decrement(filter->map[direct].map);
7079 filter->map[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7080 filter->map[direct].map = route_map;
7081 route_map_counter_increment(route_map);
7082
7083 /* Check if handling a regular peer. */
7084 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7085 /* Set override-flag and process peer route updates. */
7086 SET_FLAG(peer->filter_override[afi][safi][direct],
7087 PEER_FT_ROUTE_MAP);
7088 peer_on_policy_change(peer, afi, safi,
7089 (direct == RMAP_OUT) ? 1 : 0);
7090
7091 /* Skip peer-group mechanics for regular peers. */
7092 return 0;
7093 }
7094
7095 /*
7096 * Set configuration on all peer-group members, unless they are
7097 * explicitly overriding peer-group configuration.
7098 */
7099 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7100 /* Skip peers with overridden configuration. */
7101 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
7102 PEER_FT_ROUTE_MAP))
7103 continue;
7104
7105 /* Set configuration on peer-group member. */
7106 filter = &member->filter[afi][safi];
7107 if (filter->map[direct].name)
7108 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7109 route_map_counter_decrement(filter->map[direct].map);
7110 filter->map[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7111 filter->map[direct].map = route_map;
7112 route_map_counter_increment(route_map);
7113
7114 /* Process peer route updates. */
7115 peer_on_policy_change(member, afi, safi,
7116 (direct == RMAP_OUT) ? 1 : 0);
7117 }
7118 return 0;
7119 }
7120
7121 /* Unset route-map from the peer. */
7122 int peer_route_map_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
7123 {
7124 struct peer *member;
7125 struct bgp_filter *filter;
7126 struct listnode *node, *nnode;
7127
7128 if (direct != RMAP_IN && direct != RMAP_OUT)
7129 return BGP_ERR_INVALID_VALUE;
7130
7131 /* Unset override-flag unconditionally. */
7132 UNSET_FLAG(peer->filter_override[afi][safi][direct], PEER_FT_ROUTE_MAP);
7133
7134 /* Inherit configuration from peer-group if peer is member. */
7135 if (peer_group_active(peer)) {
7136 PEER_STR_ATTR_INHERIT(peer, peer->group,
7137 filter[afi][safi].map[direct].name,
7138 MTYPE_BGP_FILTER_NAME);
7139 PEER_ATTR_INHERIT(peer, peer->group,
7140 filter[afi][safi].map[direct].map);
7141 } else {
7142 /* Otherwise remove configuration from peer. */
7143 filter = &peer->filter[afi][safi];
7144 if (filter->map[direct].name)
7145 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7146 route_map_counter_decrement(filter->map[direct].map);
7147 filter->map[direct].name = NULL;
7148 filter->map[direct].map = NULL;
7149 }
7150
7151 /* Check if handling a regular peer. */
7152 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7153 /* Process peer route updates. */
7154 peer_on_policy_change(peer, afi, safi,
7155 (direct == RMAP_OUT) ? 1 : 0);
7156
7157 /* Skip peer-group mechanics for regular peers. */
7158 return 0;
7159 }
7160
7161 /*
7162 * Remove configuration on all peer-group members, unless they are
7163 * explicitly overriding peer-group configuration.
7164 */
7165 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7166 /* Skip peers with overridden configuration. */
7167 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
7168 PEER_FT_ROUTE_MAP))
7169 continue;
7170
7171 /* Remove configuration on peer-group member. */
7172 filter = &member->filter[afi][safi];
7173 if (filter->map[direct].name)
7174 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7175 route_map_counter_decrement(filter->map[direct].map);
7176 filter->map[direct].name = NULL;
7177 filter->map[direct].map = NULL;
7178
7179 /* Process peer route updates. */
7180 peer_on_policy_change(member, afi, safi,
7181 (direct == RMAP_OUT) ? 1 : 0);
7182 }
7183
7184 return 0;
7185 }
7186
7187 /* Set unsuppress-map to the peer. */
7188 int peer_unsuppress_map_set(struct peer *peer, afi_t afi, safi_t safi,
7189 const char *name, struct route_map *route_map)
7190 {
7191 struct peer *member;
7192 struct bgp_filter *filter;
7193 struct listnode *node, *nnode;
7194
7195 /* Set configuration on peer. */
7196 filter = &peer->filter[afi][safi];
7197 if (filter->usmap.name)
7198 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7199 route_map_counter_decrement(filter->usmap.map);
7200 filter->usmap.name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7201 filter->usmap.map = route_map;
7202 route_map_counter_increment(route_map);
7203
7204 /* Check if handling a regular peer. */
7205 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7206 /* Set override-flag and process peer route updates. */
7207 SET_FLAG(peer->filter_override[afi][safi][0],
7208 PEER_FT_UNSUPPRESS_MAP);
7209 peer_on_policy_change(peer, afi, safi, 1);
7210
7211 /* Skip peer-group mechanics for regular peers. */
7212 return 0;
7213 }
7214
7215 /*
7216 * Set configuration on all peer-group members, unless they are
7217 * explicitly overriding peer-group configuration.
7218 */
7219 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7220 /* Skip peers with overridden configuration. */
7221 if (CHECK_FLAG(member->filter_override[afi][safi][0],
7222 PEER_FT_UNSUPPRESS_MAP))
7223 continue;
7224
7225 /* Set configuration on peer-group member. */
7226 filter = &member->filter[afi][safi];
7227 if (filter->usmap.name)
7228 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7229 route_map_counter_decrement(filter->usmap.map);
7230 filter->usmap.name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7231 filter->usmap.map = route_map;
7232 route_map_counter_increment(route_map);
7233
7234 /* Process peer route updates. */
7235 peer_on_policy_change(member, afi, safi, 1);
7236 }
7237
7238 return 0;
7239 }
7240
7241 /* Unset route-map from the peer. */
7242 int peer_unsuppress_map_unset(struct peer *peer, afi_t afi, safi_t safi)
7243 {
7244 struct peer *member;
7245 struct bgp_filter *filter;
7246 struct listnode *node, *nnode;
7247
7248 /* Unset override-flag unconditionally. */
7249 UNSET_FLAG(peer->filter_override[afi][safi][0], PEER_FT_UNSUPPRESS_MAP);
7250
7251 /* Inherit configuration from peer-group if peer is member. */
7252 if (peer_group_active(peer)) {
7253 PEER_STR_ATTR_INHERIT(peer, peer->group,
7254 filter[afi][safi].usmap.name,
7255 MTYPE_BGP_FILTER_NAME);
7256 PEER_ATTR_INHERIT(peer, peer->group,
7257 filter[afi][safi].usmap.map);
7258 } else {
7259 /* Otherwise remove configuration from peer. */
7260 filter = &peer->filter[afi][safi];
7261 if (filter->usmap.name)
7262 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7263 route_map_counter_decrement(filter->usmap.map);
7264 filter->usmap.name = NULL;
7265 filter->usmap.map = NULL;
7266 }
7267
7268 /* Check if handling a regular peer. */
7269 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7270 /* Process peer route updates. */
7271 peer_on_policy_change(peer, afi, safi, 1);
7272
7273 /* Skip peer-group mechanics for regular peers. */
7274 return 0;
7275 }
7276
7277 /*
7278 * Remove configuration on all peer-group members, unless they are
7279 * explicitly overriding peer-group configuration.
7280 */
7281 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7282 /* Skip peers with overridden configuration. */
7283 if (CHECK_FLAG(member->filter_override[afi][safi][0],
7284 PEER_FT_UNSUPPRESS_MAP))
7285 continue;
7286
7287 /* Remove configuration on peer-group member. */
7288 filter = &member->filter[afi][safi];
7289 if (filter->usmap.name)
7290 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7291 route_map_counter_decrement(filter->usmap.map);
7292 filter->usmap.name = NULL;
7293 filter->usmap.map = NULL;
7294
7295 /* Process peer route updates. */
7296 peer_on_policy_change(member, afi, safi, 1);
7297 }
7298
7299 return 0;
7300 }
7301
7302 static bool peer_maximum_prefix_clear_overflow(struct peer *peer)
7303 {
7304 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
7305 return false;
7306
7307 UNSET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
7308 if (peer->t_pmax_restart) {
7309 THREAD_OFF(peer->t_pmax_restart);
7310 if (bgp_debug_neighbor_events(peer))
7311 zlog_debug(
7312 "%pBP Maximum-prefix restart timer cancelled",
7313 peer);
7314 }
7315 BGP_EVENT_ADD(peer, BGP_Start);
7316 return true;
7317 }
7318
7319 int peer_maximum_prefix_set(struct peer *peer, afi_t afi, safi_t safi,
7320 uint32_t max, uint8_t threshold, int warning,
7321 uint16_t restart, bool force)
7322 {
7323 struct peer *member;
7324 struct listnode *node, *nnode;
7325
7326 /* Set flags and configuration on peer. */
7327 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
7328
7329 if (force)
7330 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7331 else
7332 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7333
7334 if (warning)
7335 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_WARNING);
7336 else
7337 peer_af_flag_unset(peer, afi, safi,
7338 PEER_FLAG_MAX_PREFIX_WARNING);
7339
7340 peer->pmax[afi][safi] = max;
7341 peer->pmax_threshold[afi][safi] = threshold;
7342 peer->pmax_restart[afi][safi] = restart;
7343
7344 /* Check if handling a regular peer. */
7345 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7346 /* Re-check if peer violates maximum-prefix. */
7347 if ((peer_established(peer)) && (peer->afc[afi][safi]))
7348 bgp_maximum_prefix_overflow(peer, afi, safi, 1);
7349
7350 /* Skip peer-group mechanics for regular peers. */
7351 return 0;
7352 }
7353
7354 /*
7355 * Set flags and configuration on all peer-group members, unless they
7356 * are explicitly overriding peer-group configuration.
7357 */
7358 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7359 /* Skip peers with overridden configuration. */
7360 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7361 PEER_FLAG_MAX_PREFIX))
7362 continue;
7363
7364 /* Set flag and configuration on peer-group member. */
7365 member->pmax[afi][safi] = max;
7366 member->pmax_threshold[afi][safi] = threshold;
7367 member->pmax_restart[afi][safi] = restart;
7368
7369 if (force)
7370 SET_FLAG(member->af_flags[afi][safi],
7371 PEER_FLAG_MAX_PREFIX_FORCE);
7372 else
7373 UNSET_FLAG(member->af_flags[afi][safi],
7374 PEER_FLAG_MAX_PREFIX_FORCE);
7375
7376 if (warning)
7377 SET_FLAG(member->af_flags[afi][safi],
7378 PEER_FLAG_MAX_PREFIX_WARNING);
7379 else
7380 UNSET_FLAG(member->af_flags[afi][safi],
7381 PEER_FLAG_MAX_PREFIX_WARNING);
7382
7383 /* Re-check if peer violates maximum-prefix. */
7384 if ((peer_established(member)) && (member->afc[afi][safi]))
7385 bgp_maximum_prefix_overflow(member, afi, safi, 1);
7386 }
7387
7388 return 0;
7389 }
7390
7391 int peer_maximum_prefix_unset(struct peer *peer, afi_t afi, safi_t safi)
7392 {
7393 /* Inherit configuration from peer-group if peer is member. */
7394 if (peer_group_active(peer)) {
7395 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
7396 peer_af_flag_inherit(peer, afi, safi,
7397 PEER_FLAG_MAX_PREFIX_FORCE);
7398 peer_af_flag_inherit(peer, afi, safi,
7399 PEER_FLAG_MAX_PREFIX_WARNING);
7400 PEER_ATTR_INHERIT(peer, peer->group, pmax[afi][safi]);
7401 PEER_ATTR_INHERIT(peer, peer->group, pmax_threshold[afi][safi]);
7402 PEER_ATTR_INHERIT(peer, peer->group, pmax_restart[afi][safi]);
7403
7404 return 0;
7405 }
7406
7407 /* Remove flags and configuration from peer. */
7408 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
7409 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7410 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_WARNING);
7411 peer->pmax[afi][safi] = 0;
7412 peer->pmax_threshold[afi][safi] = 0;
7413 peer->pmax_restart[afi][safi] = 0;
7414
7415 /*
7416 * Remove flags and configuration from all peer-group members, unless
7417 * they are explicitly overriding peer-group configuration.
7418 */
7419 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7420 struct peer *member;
7421 struct listnode *node;
7422
7423 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
7424 /* Skip peers with overridden configuration. */
7425 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7426 PEER_FLAG_MAX_PREFIX))
7427 continue;
7428
7429 /* Remove flag and configuration on peer-group member.
7430 */
7431 UNSET_FLAG(member->af_flags[afi][safi],
7432 PEER_FLAG_MAX_PREFIX);
7433 UNSET_FLAG(member->af_flags[afi][safi],
7434 PEER_FLAG_MAX_PREFIX_FORCE);
7435 UNSET_FLAG(member->af_flags[afi][safi],
7436 PEER_FLAG_MAX_PREFIX_WARNING);
7437 member->pmax[afi][safi] = 0;
7438 member->pmax_threshold[afi][safi] = 0;
7439 member->pmax_restart[afi][safi] = 0;
7440
7441 peer_maximum_prefix_clear_overflow(member);
7442 }
7443 } else {
7444 peer_maximum_prefix_clear_overflow(peer);
7445 }
7446
7447 return 0;
7448 }
7449
7450 void peer_maximum_prefix_out_refresh_routes(struct peer *peer, afi_t afi,
7451 safi_t safi)
7452 {
7453 update_group_adjust_peer(peer_af_find(peer, afi, safi));
7454
7455 if (peer_established(peer))
7456 bgp_announce_route(peer, afi, safi, false);
7457 }
7458
7459 int peer_maximum_prefix_out_set(struct peer *peer, afi_t afi, safi_t safi,
7460 uint32_t max)
7461 {
7462 struct peer *member;
7463 struct listnode *node, *nnode;
7464
7465 /* Set flag on peer and peer-group member if any */
7466 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7467 /* Set configuration on peer. */
7468 peer->pmax_out[afi][safi] = max;
7469
7470 /* Check if handling a regular peer. */
7471 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7472 /* Skip peer-group mechanics for regular peers. */
7473 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7474 return 0;
7475 }
7476
7477 /*
7478 * Set flag and configuration on all peer-group members, unless they
7479 * are explicitly overriding peer-group configuration.
7480 */
7481 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7482 /* Skip peers with overridden configuration. */
7483 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7484 PEER_FLAG_MAX_PREFIX_OUT))
7485 continue;
7486
7487 /* Set configuration on peer-group member. */
7488 member->pmax_out[afi][safi] = max;
7489
7490 peer_maximum_prefix_out_refresh_routes(member, afi, safi);
7491 }
7492 return 0;
7493 }
7494
7495 int peer_maximum_prefix_out_unset(struct peer *peer, afi_t afi, safi_t safi)
7496 {
7497 struct peer *member;
7498 struct listnode *node;
7499 /* Inherit configuration from peer-group if peer is member. */
7500 if (peer_group_active(peer)) {
7501 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7502 PEER_ATTR_INHERIT(peer, peer->group, pmax_out[afi][safi]);
7503
7504 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7505 return 0;
7506 }
7507
7508 /* Remove flag and configuration from peer. */
7509 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7510 peer->pmax_out[afi][safi] = 0;
7511
7512 /* Check if handling a regular peer. */
7513 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7514 /* Skip peer-group mechanics for regular peers. */
7515 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7516 return 0;
7517 }
7518
7519 /*
7520 * Remove flag and configuration from all peer-group members, unless
7521 * they are explicitly overriding peer-group configuration.
7522 */
7523 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
7524 /* Skip peers with overridden configuration. */
7525 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7526 PEER_FLAG_MAX_PREFIX_OUT))
7527 continue;
7528
7529 /* Remove flag and configuration on peer-group member.
7530 */
7531 UNSET_FLAG(member->af_flags[afi][safi],
7532 PEER_FLAG_MAX_PREFIX_OUT);
7533 member->pmax_out[afi][safi] = 0;
7534
7535 peer_maximum_prefix_out_refresh_routes(member, afi, safi);
7536 }
7537 return 0;
7538 }
7539
7540 int is_ebgp_multihop_configured(struct peer *peer)
7541 {
7542 struct peer_group *group;
7543 struct listnode *node, *nnode;
7544 struct peer *peer1;
7545
7546 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7547 group = peer->group;
7548 if ((peer_sort(peer) != BGP_PEER_IBGP)
7549 && (group->conf->ttl != BGP_DEFAULT_TTL))
7550 return 1;
7551
7552 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer1)) {
7553 if ((peer_sort(peer1) != BGP_PEER_IBGP)
7554 && (peer1->ttl != BGP_DEFAULT_TTL))
7555 return 1;
7556 }
7557 } else {
7558 if ((peer_sort(peer) != BGP_PEER_IBGP)
7559 && (peer->ttl != BGP_DEFAULT_TTL))
7560 return 1;
7561 }
7562 return 0;
7563 }
7564
7565 /* Set # of hops between us and BGP peer. */
7566 int peer_ttl_security_hops_set(struct peer *peer, int gtsm_hops)
7567 {
7568 struct peer_group *group;
7569 struct peer *gpeer;
7570 struct listnode *node, *nnode;
7571 int ret;
7572
7573 zlog_debug("%s: set gtsm_hops to %d for %s", __func__, gtsm_hops,
7574 peer->host);
7575
7576 /* We cannot configure ttl-security hops when ebgp-multihop is already
7577 set. For non peer-groups, the check is simple. For peer-groups,
7578 it's
7579 slightly messy, because we need to check both the peer-group
7580 structure
7581 and all peer-group members for any trace of ebgp-multihop
7582 configuration
7583 before actually applying the ttl-security rules. Cisco really made a
7584 mess of this configuration parameter, and OpenBGPD got it right.
7585 */
7586
7587 if ((peer->gtsm_hops == BGP_GTSM_HOPS_DISABLED)
7588 && (peer->sort != BGP_PEER_IBGP)) {
7589 if (is_ebgp_multihop_configured(peer))
7590 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
7591
7592 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7593 peer->gtsm_hops = gtsm_hops;
7594
7595 /* Calling ebgp multihop also resets the session.
7596 * On restart, NHT will get setup correctly as will the
7597 * min & max ttls on the socket. The return value is
7598 * irrelevant.
7599 */
7600 ret = peer_ebgp_multihop_set(peer, MAXTTL);
7601
7602 if (ret != 0)
7603 return ret;
7604 } else {
7605 group = peer->group;
7606 group->conf->gtsm_hops = gtsm_hops;
7607 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
7608 gpeer)) {
7609 gpeer->gtsm_hops = group->conf->gtsm_hops;
7610
7611 /* Calling ebgp multihop also resets the
7612 * session.
7613 * On restart, NHT will get setup correctly as
7614 * will the
7615 * min & max ttls on the socket. The return
7616 * value is
7617 * irrelevant.
7618 */
7619 peer_ebgp_multihop_set(gpeer, MAXTTL);
7620 }
7621 }
7622 } else {
7623 /* Post the first gtsm setup or if its ibgp, maxttl setting
7624 * isn't
7625 * necessary, just set the minttl.
7626 */
7627 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7628 peer->gtsm_hops = gtsm_hops;
7629
7630 if (peer->fd >= 0)
7631 sockopt_minttl(peer->su.sa.sa_family, peer->fd,
7632 MAXTTL + 1 - gtsm_hops);
7633 if ((peer->status < Established) && peer->doppelganger
7634 && (peer->doppelganger->fd >= 0))
7635 sockopt_minttl(peer->su.sa.sa_family,
7636 peer->doppelganger->fd,
7637 MAXTTL + 1 - gtsm_hops);
7638 } else {
7639 group = peer->group;
7640 group->conf->gtsm_hops = gtsm_hops;
7641 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
7642 gpeer)) {
7643 gpeer->gtsm_hops = group->conf->gtsm_hops;
7644
7645 /* Change setting of existing peer
7646 * established then change value (may break
7647 * connectivity)
7648 * not established yet (teardown session and
7649 * restart)
7650 * no session then do nothing (will get
7651 * handled by next connection)
7652 */
7653 if (gpeer->fd >= 0
7654 && gpeer->gtsm_hops
7655 != BGP_GTSM_HOPS_DISABLED)
7656 sockopt_minttl(
7657 gpeer->su.sa.sa_family,
7658 gpeer->fd,
7659 MAXTTL + 1 - gpeer->gtsm_hops);
7660 if ((gpeer->status < Established)
7661 && gpeer->doppelganger
7662 && (gpeer->doppelganger->fd >= 0))
7663 sockopt_minttl(gpeer->su.sa.sa_family,
7664 gpeer->doppelganger->fd,
7665 MAXTTL + 1 - gtsm_hops);
7666 }
7667 }
7668 }
7669
7670 return 0;
7671 }
7672
7673 int peer_ttl_security_hops_unset(struct peer *peer)
7674 {
7675 struct peer_group *group;
7676 struct listnode *node, *nnode;
7677 int ret = 0;
7678
7679 zlog_debug("%s: set gtsm_hops to zero for %s", __func__, peer->host);
7680
7681 /* if a peer-group member, then reset to peer-group default rather than
7682 * 0 */
7683 if (peer_group_active(peer))
7684 peer->gtsm_hops = peer->group->conf->gtsm_hops;
7685 else
7686 peer->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
7687
7688 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7689 /* Invoking ebgp_multihop_set will set the TTL back to the
7690 * original
7691 * value as well as restting the NHT and such. The session is
7692 * reset.
7693 */
7694 if (peer->sort == BGP_PEER_EBGP)
7695 ret = peer_ebgp_multihop_unset(peer);
7696 else {
7697 if (peer->fd >= 0)
7698 sockopt_minttl(peer->su.sa.sa_family, peer->fd,
7699 0);
7700
7701 if ((peer->status < Established) && peer->doppelganger
7702 && (peer->doppelganger->fd >= 0))
7703 sockopt_minttl(peer->su.sa.sa_family,
7704 peer->doppelganger->fd, 0);
7705 }
7706 } else {
7707 group = peer->group;
7708 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
7709 peer->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
7710 if (peer->sort == BGP_PEER_EBGP)
7711 ret = peer_ebgp_multihop_unset(peer);
7712 else {
7713 if (peer->fd >= 0)
7714 sockopt_minttl(peer->su.sa.sa_family,
7715 peer->fd, 0);
7716
7717 if ((peer->status < Established)
7718 && peer->doppelganger
7719 && (peer->doppelganger->fd >= 0))
7720 sockopt_minttl(peer->su.sa.sa_family,
7721 peer->doppelganger->fd,
7722 0);
7723 }
7724 }
7725 }
7726
7727 return ret;
7728 }
7729
7730 static void peer_reset_message_stats(struct peer *peer)
7731 {
7732 if (peer) {
7733 atomic_store_explicit(&peer->open_in, 0, memory_order_relaxed);
7734 atomic_store_explicit(&peer->open_out, 0, memory_order_relaxed);
7735 atomic_store_explicit(&peer->update_in, 0,
7736 memory_order_relaxed);
7737 atomic_store_explicit(&peer->update_out, 0,
7738 memory_order_relaxed);
7739 atomic_store_explicit(&peer->keepalive_in, 0,
7740 memory_order_relaxed);
7741 atomic_store_explicit(&peer->keepalive_out, 0,
7742 memory_order_relaxed);
7743 atomic_store_explicit(&peer->notify_in, 0,
7744 memory_order_relaxed);
7745 atomic_store_explicit(&peer->notify_out, 0,
7746 memory_order_relaxed);
7747 atomic_store_explicit(&peer->refresh_in, 0,
7748 memory_order_relaxed);
7749 atomic_store_explicit(&peer->refresh_out, 0,
7750 memory_order_relaxed);
7751 atomic_store_explicit(&peer->dynamic_cap_in, 0,
7752 memory_order_relaxed);
7753 atomic_store_explicit(&peer->dynamic_cap_out, 0,
7754 memory_order_relaxed);
7755 }
7756 }
7757
7758 /*
7759 * If peer clear is invoked in a loop for all peers on the BGP instance,
7760 * it may end up freeing the doppelganger, and if this was the next node
7761 * to the current node, we would end up accessing the freed next node.
7762 * Pass along additional parameter which can be updated if next node
7763 * is freed; only required when walking the peer list on BGP instance.
7764 */
7765 int peer_clear(struct peer *peer, struct listnode **nnode)
7766 {
7767 if (!CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN)
7768 || !CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHUTDOWN)) {
7769 if (peer_maximum_prefix_clear_overflow(peer))
7770 return 0;
7771
7772 peer->v_start = BGP_INIT_START_TIMER;
7773 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
7774 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
7775 BGP_NOTIFY_CEASE_ADMIN_RESET);
7776 else
7777 bgp_session_reset_safe(peer, nnode);
7778 }
7779 return 0;
7780 }
7781
7782 int peer_clear_soft(struct peer *peer, afi_t afi, safi_t safi,
7783 enum bgp_clear_type stype)
7784 {
7785 struct peer_af *paf;
7786
7787 if (!peer_established(peer))
7788 return 0;
7789
7790 if (!peer->afc[afi][safi])
7791 return BGP_ERR_AF_UNCONFIGURED;
7792
7793 peer->rtt = sockopt_tcp_rtt(peer->fd);
7794
7795 if (stype == BGP_CLEAR_SOFT_OUT || stype == BGP_CLEAR_SOFT_BOTH) {
7796 /* Clear the "neighbor x.x.x.x default-originate" flag */
7797 paf = peer_af_find(peer, afi, safi);
7798 if (paf && paf->subgroup
7799 && CHECK_FLAG(paf->subgroup->sflags,
7800 SUBGRP_STATUS_DEFAULT_ORIGINATE))
7801 UNSET_FLAG(paf->subgroup->sflags,
7802 SUBGRP_STATUS_DEFAULT_ORIGINATE);
7803
7804 bgp_announce_route(peer, afi, safi, false);
7805 }
7806
7807 if (stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) {
7808 if (CHECK_FLAG(peer->af_cap[afi][safi],
7809 PEER_CAP_ORF_PREFIX_SM_ADV)
7810 && (CHECK_FLAG(peer->af_cap[afi][safi],
7811 PEER_CAP_ORF_PREFIX_RM_RCV)
7812 || CHECK_FLAG(peer->af_cap[afi][safi],
7813 PEER_CAP_ORF_PREFIX_RM_OLD_RCV))) {
7814 struct bgp_filter *filter = &peer->filter[afi][safi];
7815 uint8_t prefix_type;
7816
7817 if (CHECK_FLAG(peer->af_cap[afi][safi],
7818 PEER_CAP_ORF_PREFIX_RM_RCV))
7819 prefix_type = ORF_TYPE_PREFIX;
7820 else
7821 prefix_type = ORF_TYPE_PREFIX_OLD;
7822
7823 if (filter->plist[FILTER_IN].plist) {
7824 if (CHECK_FLAG(peer->af_sflags[afi][safi],
7825 PEER_STATUS_ORF_PREFIX_SEND))
7826 bgp_route_refresh_send(
7827 peer, afi, safi, prefix_type,
7828 REFRESH_DEFER, 1,
7829 BGP_ROUTE_REFRESH_NORMAL);
7830 bgp_route_refresh_send(
7831 peer, afi, safi, prefix_type,
7832 REFRESH_IMMEDIATE, 0,
7833 BGP_ROUTE_REFRESH_NORMAL);
7834 } else {
7835 if (CHECK_FLAG(peer->af_sflags[afi][safi],
7836 PEER_STATUS_ORF_PREFIX_SEND))
7837 bgp_route_refresh_send(
7838 peer, afi, safi, prefix_type,
7839 REFRESH_IMMEDIATE, 1,
7840 BGP_ROUTE_REFRESH_NORMAL);
7841 else
7842 bgp_route_refresh_send(
7843 peer, afi, safi, 0, 0, 0,
7844 BGP_ROUTE_REFRESH_NORMAL);
7845 }
7846 return 0;
7847 }
7848 }
7849
7850 if (stype == BGP_CLEAR_SOFT_IN || stype == BGP_CLEAR_SOFT_BOTH
7851 || stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) {
7852 /* If neighbor has soft reconfiguration inbound flag.
7853 Use Adj-RIB-In database. */
7854 if (!bgp_soft_reconfig_in(peer, afi, safi)) {
7855 /* If neighbor has route refresh capability, send route
7856 refresh
7857 message to the peer. */
7858 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
7859 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
7860 bgp_route_refresh_send(
7861 peer, afi, safi, 0, 0, 0,
7862 BGP_ROUTE_REFRESH_NORMAL);
7863 else
7864 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED;
7865 }
7866 }
7867
7868 if (stype == BGP_CLEAR_MESSAGE_STATS)
7869 peer_reset_message_stats(peer);
7870
7871 return 0;
7872 }
7873
7874 /* Display peer uptime.*/
7875 char *peer_uptime(time_t uptime2, char *buf, size_t len, bool use_json,
7876 json_object *json)
7877 {
7878 time_t uptime1, epoch_tbuf;
7879 struct tm tm;
7880
7881 /* If there is no connection has been done before print `never'. */
7882 if (uptime2 == 0) {
7883 if (use_json) {
7884 json_object_string_add(json, "peerUptime", "never");
7885 json_object_int_add(json, "peerUptimeMsec", 0);
7886 } else
7887 snprintf(buf, len, "never");
7888 return buf;
7889 }
7890
7891 /* Get current time. */
7892 uptime1 = monotime(NULL);
7893 uptime1 -= uptime2;
7894 gmtime_r(&uptime1, &tm);
7895
7896 if (uptime1 < ONE_DAY_SECOND)
7897 snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
7898 tm.tm_sec);
7899 else if (uptime1 < ONE_WEEK_SECOND)
7900 snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
7901 tm.tm_min);
7902 else if (uptime1 < ONE_YEAR_SECOND)
7903 snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7,
7904 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
7905 else
7906 snprintf(buf, len, "%02dy%02dw%dd", tm.tm_year - 70,
7907 tm.tm_yday / 7,
7908 tm.tm_yday - ((tm.tm_yday / 7) * 7));
7909
7910 if (use_json) {
7911 epoch_tbuf = time(NULL) - uptime1;
7912 json_object_string_add(json, "peerUptime", buf);
7913 json_object_int_add(json, "peerUptimeMsec", uptime1 * 1000);
7914 json_object_int_add(json, "peerUptimeEstablishedEpoch",
7915 epoch_tbuf);
7916 }
7917
7918 return buf;
7919 }
7920
7921 void bgp_master_init(struct thread_master *master, const int buffer_size,
7922 struct list *addresses)
7923 {
7924 qobj_init();
7925
7926 memset(&bgp_master, 0, sizeof(bgp_master));
7927
7928 bm = &bgp_master;
7929 bm->bgp = list_new();
7930 bm->listen_sockets = list_new();
7931 bm->port = BGP_PORT_DEFAULT;
7932 bm->addresses = addresses;
7933 bm->master = master;
7934 bm->start_time = monotime(NULL);
7935 bm->t_rmap_update = NULL;
7936 bm->rmap_update_timer = RMAP_DEFAULT_UPDATE_TIMER;
7937 bm->v_update_delay = BGP_UPDATE_DELAY_DEF;
7938 bm->v_establish_wait = BGP_UPDATE_DELAY_DEF;
7939 bm->terminating = false;
7940 bm->socket_buffer = buffer_size;
7941 bm->wait_for_fib = false;
7942 bm->tcp_dscp = IPTOS_PREC_INTERNETCONTROL;
7943 bm->inq_limit = BM_DEFAULT_INQ_LIMIT;
7944
7945 bgp_mac_init();
7946 /* init the rd id space.
7947 assign 0th index in the bitfield,
7948 so that we start with id 1
7949 */
7950 bf_init(bm->rd_idspace, UINT16_MAX);
7951 bf_assign_zero_index(bm->rd_idspace);
7952
7953 /* mpls label dynamic allocation pool */
7954 bgp_lp_init(bm->master, &bm->labelpool);
7955
7956 bgp_l3nhg_init();
7957 bgp_evpn_mh_init();
7958 QOBJ_REG(bm, bgp_master);
7959 }
7960
7961 /*
7962 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7963 * instance delete (non-default only) or BGP exit.
7964 */
7965 static void bgp_if_finish(struct bgp *bgp)
7966 {
7967 struct vrf *vrf;
7968 struct interface *ifp;
7969
7970 vrf = bgp_vrf_lookup_by_instance_type(bgp);
7971
7972 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW || !vrf)
7973 return;
7974
7975 FOR_ALL_INTERFACES (vrf, ifp) {
7976 struct listnode *c_node, *c_nnode;
7977 struct connected *c;
7978
7979 for (ALL_LIST_ELEMENTS(ifp->connected, c_node, c_nnode, c))
7980 bgp_connected_delete(bgp, c);
7981 }
7982 }
7983
7984 static void bgp_viewvrf_autocomplete(vector comps, struct cmd_token *token)
7985 {
7986 struct vrf *vrf = NULL;
7987 struct listnode *next;
7988 struct bgp *bgp;
7989
7990 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
7991 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
7992
7993 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
7994 if (bgp->inst_type != BGP_INSTANCE_TYPE_VIEW)
7995 continue;
7996
7997 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, bgp->name));
7998 }
7999 }
8000
8001 static void bgp_instasn_autocomplete(vector comps, struct cmd_token *token)
8002 {
8003 struct listnode *next, *next2;
8004 struct bgp *bgp, *bgp2;
8005 char buf[11];
8006
8007 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
8008 /* deduplicate */
8009 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next2, bgp2)) {
8010 if (bgp2->as == bgp->as)
8011 break;
8012 if (bgp2 == bgp)
8013 break;
8014 }
8015 if (bgp2 != bgp)
8016 continue;
8017
8018 snprintf(buf, sizeof(buf), "%u", bgp->as);
8019 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, buf));
8020 }
8021 }
8022
8023 static const struct cmd_variable_handler bgp_viewvrf_var_handlers[] = {
8024 {.tokenname = "VIEWVRFNAME", .completions = bgp_viewvrf_autocomplete},
8025 {.varname = "instasn", .completions = bgp_instasn_autocomplete},
8026 {.completions = NULL},
8027 };
8028
8029 struct frr_pthread *bgp_pth_io;
8030 struct frr_pthread *bgp_pth_ka;
8031
8032 static void bgp_pthreads_init(void)
8033 {
8034 assert(!bgp_pth_io);
8035 assert(!bgp_pth_ka);
8036
8037 struct frr_pthread_attr io = {
8038 .start = frr_pthread_attr_default.start,
8039 .stop = frr_pthread_attr_default.stop,
8040 };
8041 struct frr_pthread_attr ka = {
8042 .start = bgp_keepalives_start,
8043 .stop = bgp_keepalives_stop,
8044 };
8045 bgp_pth_io = frr_pthread_new(&io, "BGP I/O thread", "bgpd_io");
8046 bgp_pth_ka = frr_pthread_new(&ka, "BGP Keepalives thread", "bgpd_ka");
8047 }
8048
8049 void bgp_pthreads_run(void)
8050 {
8051 frr_pthread_run(bgp_pth_io, NULL);
8052 frr_pthread_run(bgp_pth_ka, NULL);
8053
8054 /* Wait until threads are ready. */
8055 frr_pthread_wait_running(bgp_pth_io);
8056 frr_pthread_wait_running(bgp_pth_ka);
8057 }
8058
8059 void bgp_pthreads_finish(void)
8060 {
8061 frr_pthread_stop_all();
8062 }
8063
8064 static int peer_unshut_after_cfg(struct bgp *bgp)
8065 {
8066 struct listnode *node;
8067 struct peer *peer;
8068
8069 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
8070 if (!peer->shut_during_cfg)
8071 continue;
8072
8073 if (bgp_debug_neighbor_events(peer))
8074 zlog_debug("%s: released from config-pending hold",
8075 peer->host);
8076
8077 peer->shut_during_cfg = false;
8078 if (peer_active(peer) && peer->status != Established) {
8079 if (peer->status != Idle)
8080 BGP_EVENT_ADD(peer, BGP_Stop);
8081 BGP_EVENT_ADD(peer, BGP_Start);
8082 }
8083 }
8084
8085 return 0;
8086 }
8087
8088 void bgp_init(unsigned short instance)
8089 {
8090 hook_register(bgp_config_end, peer_unshut_after_cfg);
8091
8092 /* allocates some vital data structures used by peer commands in
8093 * vty_init */
8094
8095 /* pre-init pthreads */
8096 bgp_pthreads_init();
8097
8098 /* Init zebra. */
8099 bgp_zebra_init(bm->master, instance);
8100
8101 #ifdef ENABLE_BGP_VNC
8102 vnc_zebra_init(bm->master);
8103 #endif
8104
8105 /* BGP VTY commands installation. */
8106 bgp_vty_init();
8107
8108 /* BGP inits. */
8109 bgp_attr_init();
8110 bgp_debug_init();
8111 bgp_community_alias_init();
8112 bgp_dump_init();
8113 bgp_route_init();
8114 bgp_route_map_init();
8115 bgp_scan_vty_init();
8116 bgp_mplsvpn_init();
8117 #ifdef ENABLE_BGP_VNC
8118 rfapi_init();
8119 #endif
8120 bgp_ethernetvpn_init();
8121 bgp_flowspec_vty_init();
8122
8123 /* Access list initialize. */
8124 access_list_init();
8125 access_list_add_hook(peer_distribute_update);
8126 access_list_delete_hook(peer_distribute_update);
8127
8128 /* Filter list initialize. */
8129 bgp_filter_init();
8130 as_list_add_hook(peer_aslist_add);
8131 as_list_delete_hook(peer_aslist_del);
8132
8133 /* Prefix list initialize.*/
8134 prefix_list_init();
8135 prefix_list_add_hook(peer_prefix_list_update);
8136 prefix_list_delete_hook(peer_prefix_list_update);
8137
8138 /* Community list initialize. */
8139 bgp_clist = community_list_init();
8140
8141 /* BFD init */
8142 bgp_bfd_init(bm->master);
8143
8144 bgp_lp_vty_init();
8145
8146 cmd_variable_handler_register(bgp_viewvrf_var_handlers);
8147 }
8148
8149 void bgp_terminate(void)
8150 {
8151 struct bgp *bgp;
8152 struct peer *peer;
8153 struct listnode *node, *nnode;
8154 struct listnode *mnode, *mnnode;
8155
8156 QOBJ_UNREG(bm);
8157
8158 /* Close the listener sockets first as this prevents peers from
8159 * attempting
8160 * to reconnect on receiving the peer unconfig message. In the presence
8161 * of a large number of peers this will ensure that no peer is left with
8162 * a dangling connection
8163 */
8164
8165 bgp_close();
8166 /* reverse bgp_master_init */
8167 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
8168 bgp_close_vrf_socket(bgp);
8169 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8170 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)) {
8171 if (bgp_debug_neighbor_events(peer))
8172 zlog_debug(
8173 "%pBP configured Graceful-Restart, skipping unconfig notification",
8174 peer);
8175 continue;
8176 }
8177 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
8178 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
8179 BGP_NOTIFY_CEASE_PEER_UNCONFIG);
8180 }
8181 }
8182
8183 if (bm->listen_sockets)
8184 list_delete(&bm->listen_sockets);
8185
8186 THREAD_OFF(bm->t_rmap_update);
8187
8188 bgp_mac_finish();
8189 }
8190
8191 struct peer *peer_lookup_in_view(struct vty *vty, struct bgp *bgp,
8192 const char *ip_str, bool use_json)
8193 {
8194 int ret;
8195 struct peer *peer;
8196 union sockunion su;
8197
8198 /* Get peer sockunion. */
8199 ret = str2sockunion(ip_str, &su);
8200 if (ret < 0) {
8201 peer = peer_lookup_by_conf_if(bgp, ip_str);
8202 if (!peer) {
8203 peer = peer_lookup_by_hostname(bgp, ip_str);
8204
8205 if (!peer) {
8206 if (use_json) {
8207 json_object *json_no = NULL;
8208 json_no = json_object_new_object();
8209 json_object_string_add(
8210 json_no,
8211 "malformedAddressOrName",
8212 ip_str);
8213 vty_json(vty, json_no);
8214 } else
8215 vty_out(vty,
8216 "%% Malformed address or name: %s\n",
8217 ip_str);
8218 return NULL;
8219 }
8220 }
8221 return peer;
8222 }
8223
8224 /* Peer structure lookup. */
8225 peer = peer_lookup(bgp, &su);
8226 if (!peer) {
8227 if (use_json) {
8228 json_object *json_no = NULL;
8229 json_no = json_object_new_object();
8230 json_object_string_add(json_no, "warning",
8231 "No such neighbor in this view/vrf");
8232 vty_json(vty, json_no);
8233 } else
8234 vty_out(vty, "No such neighbor in this view/vrf\n");
8235 return NULL;
8236 }
8237
8238 return peer;
8239 }
8240
8241 void bgp_gr_apply_running_config(void)
8242 {
8243 struct peer *peer = NULL;
8244 struct bgp *bgp = NULL;
8245 struct listnode *node, *nnode;
8246 bool gr_router_detected = false;
8247
8248 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
8249 zlog_debug("[BGP_GR] %s called !", __func__);
8250
8251 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
8252 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8253 bgp_peer_gr_flags_update(peer);
8254 if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART))
8255 gr_router_detected = true;
8256 }
8257
8258 if (gr_router_detected
8259 && bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) {
8260 bgp_zebra_send_capabilities(bgp, true);
8261 } else if (!gr_router_detected
8262 && bgp->present_zebra_gr_state == ZEBRA_GR_ENABLE) {
8263 bgp_zebra_send_capabilities(bgp, false);
8264 }
8265
8266 gr_router_detected = false;
8267 }
8268 }
8269
8270 printfrr_ext_autoreg_p("BP", printfrr_bp);
8271 static ssize_t printfrr_bp(struct fbuf *buf, struct printfrr_eargs *ea,
8272 const void *ptr)
8273 {
8274 const struct peer *peer = ptr;
8275
8276 if (!peer)
8277 return bputs(buf, "(null)");
8278
8279 return bprintfrr(buf, "%s(%s)", peer->host,
8280 peer->hostname ? peer->hostname : "Unknown");
8281 }