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