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