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