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