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